0

JQuery How to bind data binding?

<div id="conversations-uCount">0</div>
<script type="text/javascript">
$(document).ready(function() {
    $('#conversations-uCount').data('UnreadIDs', '1');
});
</script>

How to set the binding so that at any time when the UnreadID is changed, I can run the function?

thank

+5
source share
3 answers

In jQuery 1.4.4+, an event is fired for this changeData. If this is the only data you are dealing with an object, your handler is simple, like:

$('#conversations-uCount').bind("changeData", function() {
  //data changed, do something, for example:
  alert("Data changed!, new value for UnreadIDs: " + $.data(this, 'UnreadIDs'));
});

Here you can check it out .

+4
source

I think you could do it with a simple plugin:

$.fn.dataTrigger = function(name, value, callback) {
    $(this).data(name, value);
    callback(name, value);
    return this;
};

Then:

$('#conversations-uCount').dataTrigger('UnreadIDs', '1', myFunc);

Demo: http://jsfiddle.net/karim79/q6apA/1/

+1
source

watch object.watch, , , Object.watch() ?

0

All Articles