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:

  1. update endpoint send required data. proper way it. collection should ideally have single endpoint

  2. send seperate ajax request data 1 url before fetching collection, in collection's parse method add data response fetched collection's url

  3. do 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 sure parse doesn't reset data other endpoint.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -