javascript - How to set Backbone model to an objcet's child array? -



javascript - How to set Backbone model to an objcet's child array? -

i've got backbone app gets info json array , appends html, when seek alter json info array object coundn't manage set related array model.

note: i've took illustration of backbone construction website: http://bardevblog.wordpress.com/2012/01/16/understanding-backbone-js-simple-example/

here working code:

json

[ { "id": 0, "title": "abc", "list": [ { "type": 2, "date": "21.9.2012" } ] }, { "id": 1, "title": "cba", "list": [ { "type": 2, "date": "16.8.2013" }, { "type": 3, "date": "30.9.2014" } ] }, ]

backbone:

var app = { models: {}, collections: {}, views: {}, templates:{} } app.templates.items = _.template($('#tmplt-projects').html()); app.templates.item = _.template($('#tmplt-project').html()); app.models.items = backbone.model.extend({});

collection & views:

app.collections.items = backbone.collection.extend({ model: app.models.items, url: 'json/app.json' }); app.views.items = backbone.view.extend({ el: $('#projects'), template: app.templates.items, initialize: function () { this.collection.on('sync', this.render, this); }, render: function(){ $(this.el).html(this.template()); this.addall(); }, addall: function () { this.collection.each(this.addone); }, addone: function (model) { view = new app.views.items({ model: model }); $('#projects ol', this.el).append(view.render()); } }); app.views.item = backbone.view.extend({ tagname: 'li', classname: 'row', template: app.templates.item, render: function () { homecoming $(this.el).append(this.template(this.model.tojson())) ; } });

code doesn't work when alter array's construction object , can not found solution work thing.

{ "attr1" : "3", "attr2" : "5", "items": [ { "id": 0, "title": "abc", "list": [ { "type": 2, "date": "21.9.2012" } ] }, { "id": 1, "title": "cba", "list": [ { "type": 2, "date": "16.8.2013" }, { "type": 3, "date": "30.9.2014" } ] }, ] }

but can not work code when seek convert json info this, want set backbone model items object , ignore other 2 objects. how can set items array model ?

addall: function () { console.log(this.collection.models[0].attributes.items); // returns me array of items // can not set model this.collection.each(this.addone); },

you need add together parse function collection, when json returns server homecoming items array (which array of model attributes added collection):

app.collections.items = backbone.collection.extend({ model: app.models.items, url: 'json/app.json', parse: function(response) { homecoming response.items; } });

this way collection should initialized previous json response, , attr1, attr2 should ignored.

javascript jquery arrays json backbone.js

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -