目前我正在摆弄Marionette和Backbone(因为我必须这样做)。我有一个实体列表:CompositeView
和ItemViews
,每个ItemView都有一个包含firstname
和lastname
等数据的模型。
在每个ItemView
中都有一个链接,它将通过AJAX(另一个端点!)捕获ItemView的新数据,并且应该使用其他数据列表更新视图,例如:[{"id": 1, "foo": "this is soo bar"}, {"id": 2, "foo": "a little bit of baz"}]
。该数据应该呈现在视图中(并且可以在单击时再次删除)。
去哪里最好的方法是什么?在ItemView
中添加第二个模型或将新数据添加到现有视图中?还是完全不同的东西?我感谢每一个有用的提示!
我的解决方案(种类)
以下是我最终得到的结果:当点击附加数据的链接时,关联的ItemView
会实现我定义的Backbone.Collection
对象。该集合包含端点的URL。然后,我调用collection.fetch()
方法并使用Underscore渲染该集合(大致):
var tpl = _.template(dataTemplate);
var html = tpl({ myData: {collection.models});
node.html(html);
谢谢你们和我一起思考:)
答案 0 :(得分:0)
一堆不同的解决方法,这就是我喜欢的方式
模板助手
var View = Mn.ItemView.extend({
templateHelpers: function () {
// Each property will be available in the template
return {
extraData: 'my data',
anotherModel: this.getOption('secondModel').toJSON(),
someOtherData: ''
}
}
});
new View({model: MyModel, secondModel: SecondModel});
要从父级获取额外数据到视图中,请结帐childViewOptions
通过查看数据,它是一个项目数组。因此,childView
的{{1}}可能是CompositeView