Skip to content

Instantly share code, notes, and snippets.

@KindWizzard
Created June 19, 2014 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KindWizzard/a25e1e66590cc1b096d2 to your computer and use it in GitHub Desktop.
Save KindWizzard/a25e1e66590cc1b096d2 to your computer and use it in GitHub Desktop.
Отслеживание изменений свойств (IE7+)
function WatchPropery(watch_object, property_name, callback){
var current_value = watch_object[property_name],
old_ie = document.all && document.querySelector && !document.addEventListener || document.all && !document.querySelector;
// Setter
function setFunction(value){
if(current_value !== value){
current_value = value;
callback.call(watch_object, current_value)
return current_value;
}
};
// Getter
function getFunction(){
return current_value;
};
// < IE9
if(old_ie){
var watch_wrap = document.createElement('div'),
watch_dom = watch_object;
function attachChangeEvent(){
watch_dom[property_name] = getFunction;
watch_dom[property_name].toString = getFunction;
watch_dom.attachEvent("onpropertychange", onPropertyChange);
};
function onPropertyChange(event) {
if (event.propertyName === property_name) {
watch_dom.detachEvent("onpropertychange", onPropertyChange);
setFunction(watch_dom[property_name]);
attachChangeEvent();
}
};
if(watch_dom.nodeType === undefined){
watch_dom = document.createElement('WatchPropery');
for(var read_property in watch_object){
if(Object.prototype.hasOwnProperty.call(watch_object, read_property)){
watch_dom[read_property] = watch_object[read_property];
}
}
}
watch_wrap.appendChild(watch_dom);
attachChangeEvent();
watch_wrap.removeChild(watch_dom);
return watch_dom;
}
// > IE9, Chrome, Firefox > 3, Opera > 12
if(Object.defineProperty){
Object.defineProperty(watch_object, property_name, {
get: getFunction,
set: setFunction
});
return watch_object;
}
// Opera 10-12, Firefox 3
if(typeof {}.__defineGetter__ === 'function'){
watch_object.__defineGetter__(property_name, getFunction);
watch_object.__defineSetter__(property_name, setFunction);
return watch_object;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment