javascript - Model.fetch() callback not working after separating Backbone.js objects with RequireJS -
i'm splitting working backbone.js application, separate files models, views , routers.
in router, create model, perform .fetch() , expect reach callback function. this, however, never happens.
return backbone.router.extend({ self : this, initialize: function(){ require(['models/mymodel'], function(mymodel) { var mymodel = new mymodel(); mymodel.fetch({ success: self.callback, error: self.callback }); console.log(mymodel.get('myattr')); //prints 'undefined', although can seen in model.attributes } ); }, callback: function(){ console.log('callback reached!); //is never printed } }); models/mymodel.js:
define(function (require) { "use strict"; var $ = require('jquery'), backbone = require('backbone'); return backbone.model.extend({ url: function(){ return '/apicall'; } }); });
one major problem in code bit:
self : this, the self field created in object literal not value sensible want do. (and how value anyway?) @ beginning of initialize function. removing line above code , setting self @ beginning of initialize should give want:
initialize: function(){ var self = this; // rest identical.
Comments
Post a Comment