backbone.js - backbone-relational: usage with standalone model without relations -
i using backbone-relational. usage onetomany model works fine.
but having troubles using backbone-relational single/standalone model:
window.beeronlineshopposition = backbone.relationalmodel.extend({ urlroot:"../api/beeronlineshoppositions", idattribute: 'id', relations: [ ], defaults:{ "id":null, "position_amount":"" } }); window.beeronlineshoppositioncollection = backbone.collection.extend({ model:beeronlineshopposition, url:"../api/beeronlineshoppositions" });
in main have:
beeronlineshopproductdetails:function (id) { var beeronlineshopproduct = beeronlineshopproduct.findorcreate(id); beeronlineshopproduct.fetch(); var beeronlineshopproduct_view = new beeronlineshopproductview({el: $('#content'), model: beeronlineshopproduct}); },
so, when jump existing records (...beeronlineshop/beeronlineshopproducts/#4), nothing happens. debugging shows, fetch executed , view gets loaded. view not rendered somehow.
when refresh (f5), view gets rendered correctly.
as mentioned whole thing works one-to-many model, question is:
did make trivial syntax-error on model part?... or there other obvious reason troubles have?
it may because of asynchronous request created findorcreate
. beeronlineshopproduct.fetch()
making request server view being rendered before server returns response.
you have 2 options. can pass in rendering of view callback upon fetch
's success so:
beeronlineshop.fetch({ success: function() { var beeronlineshopproduct_view = new beeronlineshopproductview({el: $('#content'), model: beeronlineshopproduct}) });
or can pass initializer in beeronlineshopproductview listens model syncing server, , calls view re-render itself, so:
window.beeronlineshopproductview = backbone.view.extend({ initialize: function() { this.listento (this.model, "sync", this.render) }, })
Comments
Post a Comment