Skip to content

Instantly share code, notes, and snippets.

@AlexPetrov7311
Created December 6, 2017 13:01
Show Gist options
  • Select an option

  • Save AlexPetrov7311/4b9ebe89a6c785b592699818352f7bd7 to your computer and use it in GitHub Desktop.

Select an option

Save AlexPetrov7311/4b9ebe89a6c785b592699818352f7bd7 to your computer and use it in GitHub Desktop.
watcher for variable BACKEND.components.catalogGrid
var watch = function (context, setter) {
return function (varName, setter) {
var value = undefined;
Object.defineProperty(context, varName, {
get: function () { return value; },
set: function (v) {
value = v;
console.log('Value changed! New value: ' + value);
setter(value);
}
});
};
};
// Demo
var varw = watch(window);
varw('BACKEND', (v)=> {
console.log('backend setter = ', v)
var l = watch(v.components );
l('catalogGrid', v => {
console.log('catalogGrid setter = ', v)
v.styleGrid = 'grid';
});
})
BACKEND = { components: {} }
BACKEND.components.catalogGrid = { name: 'grid', styleGrid: 'list'}
'finish';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment