【发布时间】:2020-10-16 17:10:57
【问题描述】:
我需要将来自两个不同 RESTful 端点的数据与骨干网结合起来。我正在尝试使用 Backbone.RelationalModel 扩展。所以我有以下内容:
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();
flights 模型通过 'pilot_id' 键有一个指向 Pilots 模型的指针。飞行员模型将有飞行员的名字。这里的某处主干需要从 Pilots RESTful 端点获取飞行员数据。但我看不到在哪里/如何触发该提取。
【问题讨论】:
标签: rest backbone.js backbone-relational