javascript - Backbone.js render a view after a getting return value -


in code below need render view after have got valuea call on gmaps navigator. until have latitude amd longitude can't render view. followinag answers here i'm stuck error

object [object global] has no method 'afterrender suggestion.

initialize: function() {     $.when(this.beforerender).then(function(){         this.afterrender();     }); },  beforerender: function () {     if (navigator.geolocation) {         navigator.geolocation.getcurrentposition(this.onsuccessupdatepos,                                                   this.onfailupdatepos);     } else {navigator.geolocation.getcurrentposition(this.onsuccessupdatepos,                                                       this.onfailupdatepos);      } },  afterrender: function () {     this.render(); }, 

update: did told, , seems sequence beforerender-> render-> afterrender respected. added of initialization logic within function initialize(). problem putting functions in sequence appears not ensure fact function render() launched once acquired results.

i tried

    $.when(this.beforerender).then(that.render); 

as suggested me still keeps giving me errors. seems beforerender isn't launched. error keep showing me is

uncaught syntaxerror: unexpected token u

i think happen because of sessionstorage & localstorage , these not filled before render function launched. there effective way wait these results returned?

i think followed tutorial, since both beforerender , afterrender don't exist in backbone core. but, implemented people need events fired before , after rendering routine.

moreover, backbone leaves render function of view class empty, it's implement (and call it) when need.

as sidenote, it's not call render() right initialize() function, since you'll need have more control on when , how render view after having initialized it.

that said, suggest:

  • move of initialization logic (like navigator checks) inside initialize();
  • call render() out of view (right after new viewname() call)

if need (maybe further development of code) before , after callbacks, can make use of underscore (available since it's dependency) "wrap" function. such approach consists of:

initialize: function()      this.render = _.wrap(this.render, function(render) {         this.beforerender();         render();                                this.afterrender();     });  },  render: function() {}, beforerender: function() {}, afterrender: function() {} 

with in mind, go (remember specify onfailupdatepos on view class):

initialize: function() {     if (navigator.geolocation) {         navigator.geolocation.getcurrentposition(this.render, this.onfailupdatepos);     } else {         navigator.geolocation.getcurrentposition(this.render, this.onfailupdatepos);     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -