Backbone.js 和 jQuery-UI 的自定义 Widget 不能和平相处吗?
我正在寻找一种方法来使用 jQuery 的自定义小部件(用于封装我的 UI 组件以及旨在在外部 DOM 上使用的内部状态和自定义事件)以及项目中精彩的“Backbone.js”框架。 我立即偶然发现的一个主要问题是,如果我将事件处理程序绑定到 Widget 的根元素(为了控制 Widget 的内部行为),然后将同一元素设置为 Backbone 视图的根元素,Backbone 会自动取消绑定 所有预先存在的事件(由我的小部件设置)并将其替换为在视图的“事件”哈希中指定的事件处理程序。
因此,如果我在小部件声明中设置以下事件处理程序:
var el = this.element;
el.bind("mouseenter", function (e) { el.css("backgroundImage", "url(over.png)").addClass("selected"); }).bind("mouseleave", function (e) { el.css("backgroundImage", "").removeClass("selected"); });
然后实例化我的backbone.js 视图:
// "Participant"'s render() creates a DIV and initializes my widget on it
var userView = new this.Views.Participant({ model: user });
$("#somediv").append(userView.render().el);
我的鼠标事件处理程序停止响应! 有没有办法让 Backbone 管理自己的处理程序而不影响其他处理程序?
I am looking for a way to use both jQuery's custom widgets (for encapsulating my UI components along with internal states and custom events aimed for use on the outside DOM), and the wonderful "Backbone.js" framework in my project.
One major problem I immediately stumbled upon is that if I bind event handlers to my Widget's root element (in order to control the Widget's INTERNAL behavior), and later set that same element as the root element for Backbone's View, Backbone automatically unbinds ALL pre-existing events (set by my Widget) and replaces them with event handlers specified at the View's "events" hash.
So, if I set the following event handlers in my widget declaration:
var el = this.element;
el.bind("mouseenter", function (e) { el.css("backgroundImage", "url(over.png)").addClass("selected"); }).bind("mouseleave", function (e) { el.css("backgroundImage", "").removeClass("selected"); });
And then instantiate my backbone.js view:
// "Participant"'s render() creates a DIV and initializes my widget on it
var userView = new this.Views.Participant({ model: user });
$("#somediv").append(userView.render().el);
My mouse event handlers stop responding!
Is there a way to make Backbone manage it's own handlers without affecting others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题在backbone.js的开发版本中得到了解决(当前稳定版本3.3仍然包含这个“bug”)。您可以使用 https://github.com/documentcloud/backbone 中的快照。
This problem is solved in development version of backbone.js (current stable version 3.3 still contains this "bug"). You can use snapshot from https://github.com/documentcloud/backbone.