Jquery听非dom对象更改事件? [英] Jquery listen to non dom object change event?

查看:68
本文介绍了Jquery听非dom对象更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,它会不断更改用户操作以跟踪网格,所以我认为如果对象发生变化可能会触发事件?
我知道这可以做到但是我们可以不在对象上注册一个事件,一旦对象改变,我们就可以触发调用处理对象状态的函数的事件吗?

I have an object which does change on user action continuously to keep track of the grid so i thought that if its possible to trigger an event as soon as the object changes? I know this can be done but can we just not register an event on object and as soon as the object changes we can trigger the event which calls a function dealing with the state of object?

我想出了什么工作,但每次我改变对象时都必须手动触发,我们可以坐在那里等待对象改变并采取行动吗?

What i have came up with works but i have to manually trigger each time i change object can we just sit there and wait for object to change and act upon it ?

 $( requestProcessStatus ).trigger ("valueChanged"); // trigger this each time object changes` 

$( requestProcessStatus ).bind("valueChanged", function() {
console.log("object changed");
}); // do whatever in here 


 requestProcessStatus : {
    RequestNumber : '',
    ProcessStatus : '',
    LoanTrackingInfo : [
      LocalNumber: '',
      ProcessStatus: ''
    ]
},

我有这个对象,一旦用户在文本框中输入LocalNumber,我就会激活 ajax 请求,如果响应成功,LoantrackingInfo数组会填充LocalNumber和proccessStatus =成功,它可以扩展到无限。一旦对象发生变化,我想反映网格中的那些变化,这些变化会在网格中显示所有localnumber及其对应的状态?

I have this object and as soon as user enters the LocalNumber in textbox i fire up ajax request and if the response is successful the LoantrackingInfo array gets populated with LocalNumber and the proccessStatus= Success and it can scale upto infinite. As soon as the object changes i want to reflect those changes in my grid which displays all the localnumber and its corresponding status in grid?

那么只能听对象更改事件吗?

So is it possible to just listen to the object change event ?

推荐答案

你可以在方法中包装set + trigger:

You could wrap the "set + trigger" inside a method :

requestProcessStatus.setVal = function( attr, value ){
   this[attr] = value;
   $(this).trigger('valueChanged');
}

显然,您将有责任仅通过此方法修改对象你希望事件总是触发......

Obviously, you will have the responsibility to only modify the object through this method if you want the event to always trigger ...

这篇关于Jquery听非dom对象更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆