单击时呈现视图事件
Render view on click event
有没有办法让复合视图中包含的 ItemViews 仅在单击按钮时呈现?我想对集合进行更改以更新复合视图的 dom,但每个单独的 ItemView 都不应呈现,直到需要为止。
如果我的描述有点模糊,请原谅我,但我对骨干和木偶的了解非常有限。
众所周知,木偶渴望获取您的复合(或集合)视图的子视图并生成它们。这就是为什么复合视图render
方法中包含的是_renderChildren
过程。调用后,实际上无法有选择地呈现子视图。
但是有一个后门可以渲染你的整个收藏。这很简单,就像initializing
具有空集合的复合视图一样,如下所示
//Define MyCollection` and MyCompositieView and then...
var myCollection = new MyCollection(); // Construct an empty collection
var myCompositeView = new MyCompositeView({ collection: myCollection });
"空"复合视图将正常呈现自己的模板,只需跳过_renderChildren
。
然后,您可以连线事件以调用 myCompositeView.collection.add(model)
。您会注意到木偶会监听您收藏的add
事件,
_initialEvents: function() {
if (this.collection) {
this.listenTo(this.collection, 'add', this._onCollectionAdd);
// Other _initialEvents methods...
}
},
_onCollectionAdd
负责渲染添加的模型:
_onCollectionAdd: function(child) {
this.destroyEmptyView();
var ChildView = this.getChildView(child);
var index = this.collection.indexOf(child);
this.addChild(child, ChildView, index); // The rendering happens here
},
将一切整合在一起
要完成这项工作,您必须在 CompositeView 中拥有一个模型数组,但在该视图的集合之外。我通常只是连接一个$.getJSON
(或任何其他 AJAX 方法)来获取数据并将其存储在 View 对象的属性中。假设您在初始化时执行此操作:
initialize: function() {
var that = this,
dataUrl = "some/url";
$.getJSON(dataUrl, function(data) {
that.myModels = data;
});
},
而且,在复合视图中,您可能会遇到一个事件,例如单击复合视图的元素:
events: {
'click button': 'addChild'
}
addChild: function (event) {
// functionality to identify which child to add to the collection
this.collection.add(this.myModels[j]); // Where 'j' is the index the model you want lives in.
});
当调用addChild
集合时,集合会添加正确的模型,Mariontte 确保呈现使用此模型填充的子视图。
如何执行此操作会有所不同,并且您不必在视图中连接事件。但我想我证明了如何让方法独立呈现。如果你提供更多信息,我可以给你更多的想法。
相关文章:
- 取消绑定主干视图事件
- 更改el属性时未激发主干视图事件
- angularjs中的SyncFusion树视图事件
- 在视图被替换后,主干视图事件未激发,然后放回页面上
- 主干视图事件绑定较晚
- 主干视图事件未触发 - 不知道原因
- Aurelia:调整大小/布局更改视图事件
- 主干 - 在模型更改时取消注册视图事件
- 在单页应用程序上实现Google Analytics页面视图事件的行为很奇怪
- 单击时呈现视图事件
- 木偶的复合视图事件未触发
- 主干,触发视图事件
- 主干视图事件仅在视图呈现后触发一次
- Dropzone.js AMD内部Backbone视图事件
- 提升骨干.js视图事件
- ExtjsMVC嵌套视图事件和异步函数
- 复选框和标签的主干视图事件处理程序
- 主干-从子视图触发父视图事件
- 骨干视图事件没有触发
- 在父元素上注册视图事件