Quickly generate getters and setters for multiple attributes. Using the "Observable" plugin will work for the same purpose of WatchJS, but working on all browsers. The "Observable" plugin let you know everytime some object change its value.
##Generating default Getters and Setters
var MyClass = function(){
var public = this;
var private = {};
var secret = {};
public.aPublicAttr = "im am public";
private.aPrivateAttr = "im private but will have a getter and setter";
secret.aPrivateWithoutGetterOrSetter = "no one can access me!";
MultiGetSet({
public: public,
private: private
});
};
var myObj = new MyClass();
var thatPrivate = myObj.get("aPrivateAttr");
myObj.set("aPrivateAttr", thatPrivate+". works!");
alert(myObj.get("aPrivateAttr"));
##Generating observable setters and default getter
var MyClass = function(){
var public = this;
var private = {};
var secret = {}
public.aPublicAttr = "im am public";
private.aPrivateAttr = "im private but will have a getter and setter";
secret.aPrivateWithoutGetterOrSetter = "no one can access me!";
MultiGetSet({
public: public,
private: private,
handler: Observable
});
};
var myObj = new MyClass();
var thatPrivate = myObj.get("aPrivateAttr");
myObj.listen("aPrivateAttr", function(opt){
alert(JSON.stringify(opt));
});
myObj.set("aPrivateAttr", thatPrivate+". works!"); //will trigger the listener declared above
##Generating custom getter and empty setter
var MyClass = function(){
var public = this;
var private = {};
var secret = {}
public.aPublicAttr = "im am public";
private.aPrivateAttr = "im private but will have a getter and setter";
secret.aPrivateWithoutGetterOrSetter = "no one can access me!";
MultiGetSet({
public: public,
private: private,
handler: {
init: function(){},
setter: function(){}, //i dont want a setter (if setter is null will be a default setter that works just link atr = new value
getter: function(opt){
return opt.private[opt.paramName] + " Hey you!";
}
}
});
};
var myObj = new MyClass();
var thatPrivate = myObj.get("aPrivateAttr");
alert(thatPrivate);
##Using the Add method for arrays
var MyClass = function(){
var public = this;
var private = {};
var secret = {}
public.aPublicAttr = "im am public";
private.aPrivateAttr = ["old one"];
secret.aPrivateWithoutGetterOrSetter = "no one can access me!";
MultiGetSet({
public: public,
private: private,
handler: Observable
});
};
var myObj = new MyClass();
var thatPrivate = myObj.get("aPrivateAttr");
myObj.listen("aPrivateAttr", function(opt){
alert(JSON.stringify(opt));
});
myObj.add("aPrivateAttr", "new one!"); //will trigger the listener declared above
issues for next versions: