json - Backbone.js Service Call Event Binding -


i'm new backbone, , trying request (getdivisions) , store response json 'divisions', defined in defaults. logged 'divisions' inside service call, , outside service call, seen below.

define(['underscore', 'backbone', 'service-manager', 'backbone-nested'], function(_, backbone, svgmgr) { return backbone.nestedmodel.extend({ defaults: { message: "", divisions: [] }, initialize: function () { this.getdivisions(); }, getdivisions: function() { var = this; svgmgr.interface.call('getdivisions').done(function(data) { that.set('divisions', data); console.log("inside service call: " + that.get('divisions')); }); console.log("outside service call" + this.get('divisions')); } }); });,

in dev tools, 'outside service call' log called first, returning blank array (it's default), while 'inside service call' log called after that, returning correct response data. not want.

how model run service call on initialize, when reference 'divisions' response data?

first don't need model collection, , second backbone can handle ajax call you.

so have :

var division = backbone.model.extend({     urlroot: /* url single division */,     defaults: {...} });  var divisions = backbone.collection.extend({     url: /* url divisions */,     model: division,     initialize: function () {         this.fetch({             success: function(response) {                 // result here             }         });     } }); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -