javascript - jQuery custom events on non-DOM objects - Stack Overflow

I read some code recently that does something like this:bob = {'name': 'Bob Smith',

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

What 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745573597a4633830.html

相关推荐

  • javascript - jQuery custom events on non-DOM objects - Stack Overflow

    I read some code recently that does something like this:bob = {'name': 'Bob Smith',

    21小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信