在Backbone.js的框架相同的“类”的嵌套模型? [英] Nested models of the same 'class' in the Backbone.js framework?

查看:109
本文介绍了在Backbone.js的框架相同的“类”的嵌套模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的骨干。

是否有可能定义骨干模型,其中包含相同类型的模型的列表?例如:

 为MyModel = Backbone.Model.extend({
  初始化:功能(){
    nestedMyModels:新的Array();
  },  addMyModel:功能(AMODEL){
    // code此处将推动()AMODEL到阵列
  },  渲染:功能(){
     //循环遍历数组调用render()递归
  }
});

我会再有一个观点始于递归调用来呈现()。例如:

  MyView的= Backbone.View.extend({
  渲染:功能(){
     this.model.render();
  }
});


解决方案

1非数组,但集合

你认为在模式的骨干一个阵列想在收藏

始终

现在你必须做的是的实施 MyModels 的的收集和保存它的一个实例,在你的为MyModel 的实例。

  // code简化,而不是测试
为MyModel = Backbone.Model.extend({
  初始化:功能(){
    this.nestedMyModels:新MyModels();
  },  addMyModel:函数(模型){
    this.nestedMyModels.add(模型);
  }
});MyModels = Backbone.Collection.extend({
  型号:为MyModel
});

2次使用的渲染

总您认为在渲染想在查看

和的推荐方式是,如果你有一个收藏模式更好的有一个查看为每一个。这样的集合视图将调用一个迭代模型的视图:

  // code简化,而不是测试
MyModelView = Backbone.View.extend({
  渲染:功能(){
    这$ el.html(model.get(名字));
    VAR视图=新MyModelsView({集合:this.model.nestedMyModels});
    这$ el.append(view.render.el);    返回此;
  }
});MyModelsView = Backbone.View.extend({
  渲染:功能(){
    this.collection.each(函数(模型){
      VAR视图=新MyModelView({模式:模式});
      这$ el.append(view.render.el);
    });    返回此;
  }
});

I'm new to Backbone.

Is it possible to define a Model in backbone which contains a list of Models of the same type? Example:

MyModel = Backbone.Model.extend({
  initialize: function() {
    nestedMyModels:new Array();
  },

  addMyModel: function(aModel) {
    // Code here would push() aModel onto array
  },

  render: function() {
     // Loop through array calling render() recursively
  }
});

I would then have a View which started a recursive call to render(). Example:

MyView = Backbone.View.extend({
  render:function() {
     this.model.render();
  }
});

解决方案

1 no arrays but Collections

Always that you think in an Array of Models in Backbone think in a Collection.

Now what you have to do is implement a Collection of MyModels and keep one instance of it in your MyModel instance.

// code simplified and not tested
MyModel = Backbone.Model.extend({
  initialize: function() {
    this.nestedMyModels: new MyModels();
  },

  addMyModel: function( model ) {
    this.nestedMyModels.add( model );
  }
});

MyModels = Backbone.Collection.extend({
  model: MyModel
});

2 use Views for render

Always that you think in render think in a View.

And the recommend way is that if you have a Collection and a Model better having a View for each one. This way the View of the Collection will call the View of the Model in an iteration:

// code simplified and not tested
MyModelView = Backbone.View.extend({
  render: function(){
    this.$el.html( model.get("name") );
    var view = new MyModelsView({ collection: this.model.nestedMyModels });
    this.$el.append( view.render.el );

    return this;
  }  
});

MyModelsView = Backbone.View.extend({
  render: function(){
    this.collection.each( function( model ){
      var view = new MyModelView({ model: model });
      this.$el.append( view.render.el );
    });

    return this;
  }
});

这篇关于在Backbone.js的框架相同的“类”的嵌套模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆