使用 Spine 绑定到全局事件jQuery
我是第一次尝试 Spine.js,目前正在转换使用 jQuery 的现有 js 文件。
它目前执行类似以下操作:
$('document').bind('facebook:ready', function () {
$('.myElement').click(callback);
});
它基本上等待文档上触发“facebook:ready”事件,然后将 onclick 附加到 .myElement。
到目前为止,我已经能够按照控制器文档 http://spinejs.com/ 来执行常规事件docs/controllers
myController = Spine.Controller.sub({
el: $('#mainViewElement')
, events: hashOfEventsToNamesAndHandler
});
将旧代码转换为 Spine 的正确方法是什么?而且,作为一个相关的问题,由于我有一个用于命名空间的全局对象,因此将我的“facebook:ready”事件附加到该对象而不是文档会更好吗?
我在想的一件事是,当“facebook:ready”触发时,我可以设置一个标志。然后,我使用正常的 Spine.Controller 语法将单击处理程序附加到 .myElement,当触发单击时,我检查是否设置了该标志,如果没有设置,我会立即返回。我只是不确定这是否是解决这个问题的最佳方法......
I'm trying out Spine.js for the first time and I'm currently converting an existing js file that uses jQuery.
It currently does something like this:
$('document').bind('facebook:ready', function () {
$('.myElement').click(callback);
});
Where it basically waits for the 'facebook:ready' event to be triggered on the document and then attaches an onclick to .myElement.
So far, I've been able to do regular events by following the documentation for Controllers, http://spinejs.com/docs/controllers
myController = Spine.Controller.sub({
el: $('#mainViewElement')
, events: hashOfEventsToNamesAndHandler
});
What is the right way to convert the old code to Spine? And, as a related question, since I have a global object that I use for namespacing, would it be better to attach my 'facebook:ready' event to that instead of document?
One thing I was thinking was I could have a flag that gets set when 'facebook:ready' fires. Then I attach the click handler to .myElement using the normal Spine.Controller syntax and when the click gets triggered I check to see if that flag was set, if not I return immediately. I'm just not sure if this is the best way to go about this though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
facebook:ready 事件是在 Spine 之外发生的事件,因此您将无法使用事件哈希来处理它。事件哈希中的操作的范围仅限于控制器处于活动状态的元素。
在 Spine 中,您不仅限于事件属性。你可以使用任何你想要的东西。事件哈希只是一个捷径。要设置更复杂的东西,你可以在构造函数中做一些事情。
我不知道您的应用程序的层次结构,也不知道当 facebook 事件触发时有多少控制器需要自行更新。但我们假设只有一个控制器会对其进行监视,并且它会通知其他控制器刚刚发生的情况......以防需要触发其他一些逻辑。
未经测试的代码!!
编辑:实现的代码: http://jsfiddle.net/SpoBo/zAwKk/3/
为了我的代码的 CoffeeScriptness。它确实值得学习,特别是因为您可以从 Spine 源代码中学到很多东西。
The facebook:ready event is something that happens out of Spine so you won't be able to handle it with an events hash. The actions in an events hash are scoped to the element that controller is active on.
In Spine you're not limited to the events attributes. You could use whatever you want. The events hash is only a shortcut. To set up more complex stuff you could do something in the constructor.
I don't know the hierarchy of your app and how many controllers would need to update itself when the facebook event fires. But let's assume that only one controller will monitor for it and that it informs others of what just happened... in case some other logic needs to be triggered.
Untested code!!
edit: implemented code: http://jsfiddle.net/SpoBo/zAwKk/3/
Apologies for the CoffeeScriptness of my code. It's really worth learning especially because you can learn a lot from the Spine source.