【问题标题】:Combining RESTful data in Backbone Relational Models在主干关系模型中组合 RESTful 数据
【发布时间】: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


    【解决方案1】:

    我无法让 Backbone.RelationalModel 按预期工作。所以我放弃了 RelationalModel。

    我想出的是,我为飞行员模型添加了一个集合:

     app.PilotList= app.Collection.extend({
        model: app.Pilots,
        url: POST_SUBMITTER.root + 'cloud_base/v1/pilots?role=subscriber'
     }) ; 
    

    (从模型中删除 URL。)然后在 FlightView 中更改了我的初始化方法:

    initialize: function(){
        // create the two collections
          this.pilots = new app.PilotList();
          this.collection = new app.FlightList();
         // fetch the collections separately 
         // async:false forces the app to wait until the fetch is complete. 
          this.pilots.fetch({reset:true, async:false} );
          this.collection.fetch({reset:true });    
          this.listenTo(this.pilots, 'reset', this.render);                
          this.render();
          this.listenTo(this.collection, 'add', this.renderItem);
          this.listenTo(this.collection, 'reset', this.render);
        },`
    

    然后在渲染函数中,我使用飞行模型中的“pilot_id”作为我进入飞行员模型的键,从飞行员集合复制到航班集合:

          render: function(){
            this.collection.each(function(item){
              item.set({"p_first_name":this.pilots.findWhere({pilot_id:parseInt(item.get('pilot_id'),10)}).get("first_name")}, {silent: true }); 
              item.set({"p_last_name" :this.pilots.findWhere({pilot_id:parseInt(item.get('pilot_id'), 10) }).get("last_name")}, {silent: true } ); 
              this.renderItem(item);        
        }, this );
      },
    

    现在我的飞行模型有飞行员的名字和姓氏。 {silent:true} 导致主干在设置值时不触发事件。无论如何,当 REST 端点接收到飞行员名称时,它将忽略它。我可以创建和编辑航班,而不必重新加载 Pilots 集合。无法通过此界面添加新飞行员,当添加新飞行员时,重新加载此应用程序对我的应用程序来说没什么大不了的。
    可能有更好的方法,但这对我有用。

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      相关资源
      最近更新 更多