JavaScriptでオブジェクトオブザーバーを作成しようとしています。
これは私が得る限りです:
//object to observe an object
observe : {
parent : this, //keep context of parent
references : {}, //keep track of setted references
originals : {}, // also for the originals
callStack : 0, // trying to prevent the callstack
// set an object to observe..
setObject : function ( name, object ){
this.references[name] = object;
//app.utils.extendObject is a working object extending function.
this.originals[name] = app.utils.extendObject( new Object , object );
//start watching the object
this.watchObject( name );
},
watchObject : function ( referenceName ){
this.callStack++;
//walk through reference
for( prop in this.references[referenceName] ){
// if the reference differs from the original...
if( this.references[referenceName][prop] !== this.originals[referenceName][prop] ){
//... log the differance
console.log( referenceName + " with property " + prop + " has changed." );
}
}
// set variable with call to this function
var func = eval( "app.utils.observe.watchObject(" + referenceName + ")" );
//read somewhere this could do the trick
if( callStack == 1000 ){
setTimeout( func , 100 );
}
func();
}
}
問題はコールスタック エラーです。これを観察するより良い方法はありますか? もちろん、現在 Chrome で Object.observe を使用できますが、クロスブラウザ ソリューションが必要です。
どんな助けでも大歓迎です!