事件触发器与选项中的回调
在 jQuery 插件中,您认为哪种方式最好允许函数在您的插件中挂钩 - 通过触发器或在插件函数中传递的选项(参数)?
$.trigger('myplugin_completed', someData);
$(document).bind('myplugin_completed', function(event, someData){ ... });
vs
myPluginOptions.onComplete(someData);
$('.stuff').myPlugin({onComplete: function(someData){ ... }});
In jQuery plugins, which way do you think it's best to allow a function to be hooked in your plugin - trough triggers, or options (arguments) passed in the plugin function?
$.trigger('myplugin_completed', someData);
$(document).bind('myplugin_completed', function(event, someData){ ... });
vs
myPluginOptions.onComplete(someData);
$('.stuff').myPlugin({onComplete: function(someData){ ... }});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案
我认为最好的解决方案 - 至少在您描述的情况下(也许不是在所有可能的情况下) - 是以以下方式组合两者:
并且这一行:
负责绑定事件处理程序。当有人在初始化插件时在选项中传递
onComplete
回调时,也可以调用它(当然应该调整选择器以满足初始化 pugin 的代码所使用的选择器)。总结
总而言之,您可以:
completed
),myplugin
),.on()
函数(自 jQuery 1.7 起可用且首选),onComplete
选项传递给您的插件,则在初始化插件的代码中绑定它没有问题(因此从在插件内,使用.on()
函数,将事件处理程序绑定到偶数命名空间中的事件名称)。Solution
I think the best solution - at least in the case you described (maybe not in all the possible cases) - is to combine both in the following way:
and this line:
is responsible for binding the event handler. It can be called also when someone passes
onComplete
callback within options when initializing your plugin (of course selector should be adjusted to meet the one used by the code initializing your pugin).Summary
To sum up, you could:
completed
),myplugin
),.on()
function (available and preferred since jQuery 1.7),onComplete
option is passed to your plugin, there is no problem in binding it within the code initializing the plugin (so from within the plugin, using the.on()
function, binding event handler to your event name within your even namespace).我会投票给第二个选项,因为这样就可以控制onComplete事件,而且它只绑定到元素上。将其绑定到文档并不好,因为可以执行 $(document).unbind() 来取消绑定所有事件。
I will vote for the second option, because in this way one can control the onComplete event, and it is binded only to the element. Binding it to documents it is not good, because one can do $(document).unbind(), that unbind all events.
我认为这两种方式都很好,而且没有一种比另一种有太大优势。
例如,jQuery UI 对选项使用回调,对实际事件(例如开始/停止拖动)使用事件触发器。
但是,通过在代码中创建事件触发器,您可能会创建更灵活的方式来添加更多事件处理程序,而无需修改现有代码。我的意思是,当你得到回调时,它是这样的:
如果你需要一些额外的操作,你可以将它们写入存在函数或将函数放入其中:
或者
为了使用事件进行比较,它会像:
额外的操作:
如果你不需要一些操作您只能解除它们的绑定。
它们之间有区别,但通常要根据具体情况而定,哪个更好;
I think both ways are good and none of them has big advantage over another.
For example, jQuery UI use callback for options and event triggers for actual events such as start/stop dragging.
But, by creating event triggers in code you create maybe more flexible way to add more event handlers, without modyfing existing code. I mean when you got callbacks, it goes this way:
If you need some extra actions you write them into existion function or put functions inside:
or
For comparison using events it would be like:
Extra actions:
If you don't need some actions you can unbind only them.
There are differences between them, but usually it depends on particular situation, which one is better;