在视图被替换后,主干视图事件未激发,然后放回页面上
Backbone View events not firing after the view is replaced, then put back on the page
问题:
使用$element.html(anotherView)
将视图替换为其他内容,然后使用#element.html(theView)
将视图放回页面后,在视图内部设置的事件不会触发。
示例:
var BuggyView = Backbone.View.extend({
// Event works the at first, but not after the view is replaced, then added back
// onto the page
events:{
'click #some-element': 'on_click_some_element'
},
// This function gets called before the view is replaced by another view, but
// not after it is added back into the page the second time
on_click_some_element:function(event){
alert('User clicked "some element"');
}
});
此代码执行后事件工作:
// Create the new view:
var buggyView = new BuggyView({});
// Add the view to the page
$element.html(buggyView.el);
稍后,当页面上的视图被替换为其他内容时,就会出现此代码:
$element.html(anotherView.el);
将视图添加回页面后,事件不再工作:
$element.html(buggyView.el);
很可能在删除视图时,也删除了视图的绑定事件,您可能需要重新合并事件。或者不使用(jquery).remove-use.declact.
在DOM中设置视图元素后运行render:
$element.html(anotherView.el);
anotherView.render();
$element.html(theView.el);
theView.render();
您的问题可能是渲染没有再次运行。您只是在切换DOM分配。
相关文章:
- 在Django中,可以在视图中创建一个包含js的变量,然后在循环模板标记中使用它
- 主干提取模型然后渲染视图
- 在视图被替换后,主干视图事件未激发,然后放回页面上
- 从 KnockoutJS 视图模型中获取值,然后在另一个视图模型上调用它
- 主干 - 渲染客户端视图,然后在保存时创建它们
- 如何使用 javascript 在 MVC 项目中加载视图,然后在其中加载部分视图
- 尝试检测网格视图的哪一行处于编辑模式,然后在选中复选框字段时调整文本框
- 查看 json,然后单击该 json 中的 links.json,并将其替换为当前 json 视图
- 从局部视图打开一个弹出窗口,然后重新加载父局部视图
- Knockout:将一个值从控制器传递到视图,然后从视图传递到视图模型
- 从ngResource获取数据,然后将结果绑定到视图
- 以编程方式添加视图,然后引用这些视图
- 如何重载然后重定向到另一个视图
- 主干创建集合,然后视图-异步
- 使用Angular将请求传递给DJANGO REST api,然后在DJANGO视图中返回这些结果
- Asp.net MVC动态WEBGRID搜索所有列,然后在同一视图中更新WEBGRID
- 在Ember.js中将参数传递给视图,然后传递给helper
- 如何在屏幕外加载HTML页面,然后将其滑入视图
- 从视图中设置一个cookie,然后从Rails中的Controller中读取它
- ASP.NET网格视图:用鼠标高亮显示行,然后更新高亮显示的行