触发并观察类对象的 jQuery 事件
使用 jQuery 作为我的基础,我有 2 个类。对象A&对象B。
ObjB 具有对 objA 的引用。当 ObjA.fn 执行时,我希望 objB.fn 也自动执行。我似乎无法启动 objB.fn ?我做错了什么?
IE
objA = function() {
var v = 0,
fn = function(i) {
v = i;
$(v).trigger('customEvent', i);
};
return { fn : fn };
};
objB = function(a) {
var v2 = 0,
myObjA = a,
fn = function(e, i) {
v2 = i*(-1);
};
$(a).bind('customEvent', fn);
return { fn : fn };
};
var a = new objA();
var b = new objB(a);
Using jQuery as my base, I have 2 classes. objA & objB.
ObjB has a reference to objA. when ObjA.fn is executed, I want objB.fn to automatically execute too. I can't seem to get objB.fn to fire? What am I doing wrong?
i.e.
objA = function() {
var v = 0,
fn = function(i) {
v = i;
$(v).trigger('customEvent', i);
};
return { fn : fn };
};
objB = function(a) {
var v2 = 0,
myObjA = a,
fn = function(e, i) {
v2 = i*(-1);
};
$(a).bind('customEvent', fn);
return { fn : fn };
};
var a = new objA();
var b = new objB(a);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我找到了答案。问题是我没有从正确的对象上触发事件。这都是关于范围的。 objB 可以看到 objA,因此 objA 需要触发事件,而不是它的值。这也意味着我需要监听对 objA 的引用以查看它何时触发事件。
正确示例如下:
Ok, so I found my answer. The problem was that I wasn't firing the event off the correct object. It's all about scope. objB can see objA, so objA needs to fire the event, not it's value. which also means I need to listen to my reference to objA to see when it's firing the event.
Correct example below: