最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论