admin管理员组文章数量:1026989
I read some code recently that does something like this:
bob = {'name': 'Bob Smith', 'rank': 7};
$(bob).bind("nameChanged", function () { /* ... */});
// ...
$(bob).trigger("nameChanged");
This appears to work.
But I can't find anything in the jQuery documentation or source about calling the jQuery constructor with an object that is neither a selector nor a DOM node. So my question is, is this a supported use, or is it essentially working by accident?
If you wanted to use an event-driven model for your Model or View objects in JavaScript, how would you do it?
I read some code recently that does something like this:
bob = {'name': 'Bob Smith', 'rank': 7};
$(bob).bind("nameChanged", function () { /* ... */});
// ...
$(bob).trigger("nameChanged");
This appears to work.
But I can't find anything in the jQuery documentation or source about calling the jQuery constructor with an object that is neither a selector nor a DOM node. So my question is, is this a supported use, or is it essentially working by accident?
If you wanted to use an event-driven model for your Model or View objects in JavaScript, how would you do it?
Share Improve this question asked Mar 23, 2011 at 20:28 keturnketurn 4,7983 gold badges32 silver badges40 bronze badges1 Answer
Reset to default 5What happens is that it just wraps that object in a jQuery wrapper. It then applies a callback to a new property on that object and then triggers it.
It's an odd way of doing things, but perfectly acceptable.
I read some code recently that does something like this:
bob = {'name': 'Bob Smith', 'rank': 7};
$(bob).bind("nameChanged", function () { /* ... */});
// ...
$(bob).trigger("nameChanged");
This appears to work.
But I can't find anything in the jQuery documentation or source about calling the jQuery constructor with an object that is neither a selector nor a DOM node. So my question is, is this a supported use, or is it essentially working by accident?
If you wanted to use an event-driven model for your Model or View objects in JavaScript, how would you do it?
I read some code recently that does something like this:
bob = {'name': 'Bob Smith', 'rank': 7};
$(bob).bind("nameChanged", function () { /* ... */});
// ...
$(bob).trigger("nameChanged");
This appears to work.
But I can't find anything in the jQuery documentation or source about calling the jQuery constructor with an object that is neither a selector nor a DOM node. So my question is, is this a supported use, or is it essentially working by accident?
If you wanted to use an event-driven model for your Model or View objects in JavaScript, how would you do it?
Share Improve this question asked Mar 23, 2011 at 20:28 keturnketurn 4,7983 gold badges32 silver badges40 bronze badges1 Answer
Reset to default 5What happens is that it just wraps that object in a jQuery wrapper. It then applies a callback to a new property on that object and then triggers it.
It's an odd way of doing things, but perfectly acceptable.
本文标签: javascriptjQuery custom events on nonDOM objectsStack Overflow
版权声明:本文标题:javascript - jQuery custom events on non-DOM objects - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745573596a2156885.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论