javascript - How to initialise a BackboneJS model that has no defaults? -
i've found myself doing lot of these, instance:
var item = backbone.model.extend({ }); var sampleitemcode = 12345; var item = new item({itemcode: sampleitemcode}); i have no defaults of attributes want set, , yet want make own model. looks weird have empty backbone.model that, i'm wondering if there way initialise it?
better have this.
var item = backbone.model.extend({ defaults: {itemcode: -1} }); itemcode definite need item. default it. when write functions inside item model, you'll have itemcode , can check against it. , when fetch data server collection , if item don't have itemcode or when initialize item , if failed set itemcode, it'll have it's default.
for example, if adding new item collection. can this.
collection.push(new item({name: "item 56", price: 1500})); here don't need pass itemcode: -1 on each time. it'll defaulted , when save, in server, can check itemcode , if -1, can consider new item add.
but nothing wrong have did. you'll surely add functions model when extend app.
Comments
Post a Comment