javascript - Backbone js building a collection from the results returned from multiple URL's -
i have model looks this:
var basicmodel = backbone.model.extend({ defaults: { a: '', b: '', c: '', d: '', e: '' }, idattribute: "f", parse: function (data) { return data; }, initialize: function () { console.log('intialized'); }, constructor: function (attributes, options) { backbone.model.apply(this, arguments); } });
collections this:
var basiccollection = backbone.collection.extend({ model: basicmodel, url: urlcode }); var acollection = basiccollection.extend({ parse: function (data) { return data.a.b.c.d; } }); var acollection = new acollection ();
and views this:
var basicview = backbone.view.extend({ tagname: 'tr', template: _.template($('#basic-status-template').html()), render: function () { this.$el.html(this.template(this.model.attributes)); return this; } }); var basicsview = backbone.view.extend({ initialize: function () { this.render(); }, });
this how collection fetch looks (which builds views):
acollection.fetch({ success: function () { // view var aview = basicsview.extend({ el: '#foobar #table-body', render: function () { this.$el.html(''); acollection.each(function (model) { var x = new basicview({ model: model }); this.$el.append(x.render().el); }.bind(this)); return this; } }); var app = new aview(); } });
but face problem when trying add piece of detail tables views populate. 1 of columns require data come seperate url. still want part of same process.
is there way form collection result of 2 url's. (i.e. a, b, d , e come url 1, , c comes url 2)?
this way need change template , should work same. instead of having alter load of other stuff well.
thanks.
you have few options:
update endpoint send required data. proper way it. collection should ideally have single endpoint
send seperate ajax request data 1 url before fetching collection, in collection's
parse
method add data response fetched collection's urldo like:
$.when(collection.fetch(), collection.fetchextradata()) .done(()=> { /* create view here */ });
fetchextradata
here custom function sends request , updates collection data. way both requests sent simultaneously. need make sureparse
doesn't reset data other endpoint.
Comments
Post a Comment