Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 表示作为对象和集合的itemview的主干和木偶约定_Backbone.js_Marionette - Fatal编程技术网

Backbone.js 表示作为对象和集合的itemview的主干和木偶约定

Backbone.js 表示作为对象和集合的itemview的主干和木偶约定,backbone.js,marionette,Backbone.js,Marionette,我有一个物体看起来像这样 { first_name: 'Matt', last_name: 'Damon', movies: [{name: 'mov_name1', director: 'Sven'}, {name:'mov_name2', director: 'Joe'}] } mycollection = new MyCollection(); mycollection.fetch(); mycollection.each(function(item){

我有一个物体看起来像这样

{
  first_name: 'Matt',
  last_name: 'Damon',
  movies: [{name: 'mov_name1', director: 'Sven'}, {name:'mov_name2', director: 'Joe'}]
}
  mycollection = new MyCollection();
  mycollection.fetch();
  mycollection.each(function(item){
      var actorModel = new ActorModel({firstName : item.get('first_name'),
                               lastname: item.get('last_name')});
      var moviesCollection = new MoviesCollection(item.get('movies'));
      var xCompositeView = new XCompositeView({model:xmodel,
                                               collection:moviesCollection});
  });
有一个端点将返回此数据,我将使用fetch获取此数据并将其放入模型或ItemView中。(我必须使用fetch,因为我确实需要这些对象的集合,所以我将在集合上调用fetch。)但是,对象的表示形式不应该是单独的模型,它应该是模型(或ItemView),而movies属性应该是集合,因为当我显示这个对象的行时,它应该是一个带有模型和集合的CompositeView。我的问题是,在模型上调用fetch之后,如何将此对象表示转换为模型和集合


抱歉,解释起来有点混乱…

对集合进行一次提取,然后为每个元素创建一个模型和集合,然后将其传递给compositeView

像这样的

{
  first_name: 'Matt',
  last_name: 'Damon',
  movies: [{name: 'mov_name1', director: 'Sven'}, {name:'mov_name2', director: 'Joe'}]
}
  mycollection = new MyCollection();
  mycollection.fetch();
  mycollection.each(function(item){
      var actorModel = new ActorModel({firstName : item.get('first_name'),
                               lastname: item.get('last_name')});
      var moviesCollection = new MoviesCollection(item.get('movies'));
      var xCompositeView = new XCompositeView({model:xmodel,
                                               collection:moviesCollection});
  });
通过这种方式,您的Compositeview将获得一个模型和一个集合,您可以为每部电影使用itemView正确地渲染它们


希望这有帮助。

如果使用另一个框架不是问题,那么有一个称为主干关系框架的框架。它旨在处理模型之间的关系。请参见中的动物园示例


在示例中,有一个动物园模型,模型有一个由动物模型组成的动物集合。使用Zoo模型上的单个提取,您将动物集合作为主干集合,而不是普通对象数组。

是的,这是
CollectionView
CompositeView
之间的区别。除了集合之外,CompositeView还可以使用模型呈现自己的非重复项。感谢您的建议!不过,这不是在做额外的工作吗?有没有更清晰的方式来表达这一点?我会继续进行头脑风暴,只是想知道在处理关联时是否有约定。此外,我希望演员的集合(以及他们的电影)将位于CollectionView中,因此迭代对象以创建CompositeView可能并不理想。。。如果在compositeView的初始化过程中,它有代码从该演员的电影数组中提取集合,会怎么样,类似于下面所示。我想这将是一个很好的实践,因为德里克·贝利发布了答案哈哈。你觉得怎么样?亲爱的,谢谢我会调查的。我以前从没听说过这个!你认为它能用木偶吗?是的,它能用。我的意思是,关系模型只是扩展了主干模型,所以您可以在木偶视图中安全地使用它