I need to combine data from two different RESTful endpoints with backbone.js. I'm trying to use the Backbone.RelationalModel extension. So I have the following:
app.Pilots = Backbone.RelationalModel.extend({
url: POST_SUBMITTER.root + 'cloud_base/v1/pilots',
initialize: function(){
}
});
app.Flight = Backbone.RelationalModel.extend({
initialize: function(){
},
relations: [
{
type: Backbone.HasOne,
key: 'pilot_id',
relatedModel: app.Pilots,
],
wait: true
});
app.FlightList= Backbone.Collection.extend({
model: app.Flight,
url: POST_SUBMITTER.root + 'cloud_base/v1/flights',
}) ;
app.FlightsView = Backbone.View.extend({
el: '#flights',
localDivTag: '#addFlight Div',
preinitialize(){
this.collection = new app.FlightList();
},
initialize: function(){
this.collection.fetch({reset:true});
this.render();
this.listenTo(this.collection, 'add', this.renderItem);
this.listenTo(this.collection, 'reset', this.render);
},
render: function(){
this.collection.each(function(item){
this.renderItem(item);
}, this );
},
renderItem: function(item){
var expandedView = app.FlightView.extend({ localDivTag:this.localDivTag });
var itemView = new expandedView({
model: item
})
this.$el.append( itemView.render().el);
}
});
new app.FlightsView();
The flights model has a pointer to the pilots model via a key 'pilot_id'
. The pilots model will have the name of the pilot. Somewhere in here backbone needs to fetch the pilot data from the Pilots RESTful endpoint. But I do not see where/how to trigger that fetch.