javascript - Fetch issues in backbone collection with id -
following json:
[ { "id" : "1", "type" : "report" }, { "id" : "2", "type" : "report" }, { "id" : "1", "type" : "email" }, ]
consider, json returned backbone collection -> service call. now, when i'm using json response render html table using backbone view , handlebars template system. 2 rows gets displayed, instead there should 3 rows.
note: collection parse response returning right json (i.e. 3 rows). when overwrite id
using collection parse unique random generated number 3 rows displayed. not ok, because don't want alter id
.
i want row displayed following:
1 reports 2 reports 1 email
from documentation collection add,
note adding same model (a model same id) collection more 1 time no-op.
while cannot see reason why 2 different objects should have same id, may have valid reason. 1 suggestion add together property each object in json response, _dummyid
, set autoincrementing value server side. on client side, in model definition code, set idattribute
_dummyid
.
json response,
[ { "id" : "1", "_dummyid": "1", "type" : "report" }, { "id" : "2", "_dummyid": "2", "type" : "report" }, { "id" : "1", "_dummyid": "3", "type" : "email" }, ]
your model definition, from http://backbonejs.org/#model-idattribute,
var meal = backbone.model.extend({ idattribute: "_dummyid" });
that said, hope there elegant setting in backbone, makes backbone collection acts list instead of set.
javascript jquery backbone.js underscore.js handlebars.js
No comments:
Post a Comment