public
Last active

MultiGetSet.JS - Quickly generate getters and setters for multiple attributes

  • Download Gist
MultiGetSet.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
var MultiGetSet = function(opt){
 
var getType = function(o) {
return ({}).toString.call(o).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
};
 
if(!opt.public || !opt.private)
return opt.public;
 
if(opt.handler && opt.handler.init)
opt.handler.init(opt);
 
if(!opt.handler || !opt.handler.setter){
opt.public.set = function(paramName, newValue){
opt.private[paramName] = newValue;
};
}else{
opt.public.set = function(paramName, newValue){
return opt.handler.setter({
public: opt.public,
private: opt.private,
paramName: paramName,
newValue: newValue,
caller: arguments.callee.caller
});
};
}
 
 
if(!opt.handler || !opt.handler.getter){
opt.public.get = function(paramName){
return opt.private[paramName];
};
}else{
opt.public.get = function(paramName){
return opt.handler.getter({
public: opt.public,
private: opt.private,
paramName: paramName,
caller: arguments.callee.caller
});
};
}
 
 
 
if(!opt.handler || !opt.handler.adder){
opt.public.add = function(paramName, newValue){
if(getType(opt.private[paramName])==="array")
opt.private[paramName].push(newValue);
};
}else{
opt.public.add = function(paramName, newValue){
if(getType(opt.private[paramName])==="array")
return opt.handler.adder({
public: opt.public,
private: opt.private,
paramName: paramName,
newValue: newValue,
caller: arguments.callee.caller
});
};
}
 
return opt.public;
};
Observable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
var Observable = {
 
 
clone: function(o){
if(o == null || typeof(o) != 'object')
return o;
 
var temp = o.constructor(); // changed
 
for(var key in o)
temp[key] = Observable.clone(o[key]);
return temp;
},
 
init: function(opt){
opt.public.listeners = {};
 
opt.public.listen = function(paramName, callback){
if(Object.prototype.toString.call(paramName) === '[object Array]')
for(var p in paramName){
opt.public.listenOne(paramName[p], callback);
}
else
opt.public.listenOne(paramName, callback);
};
 
opt.public.listenOne = function(paramName, callback){
if(!opt.public.listeners[paramName])
opt.public.listeners[paramName] = [];
opt.public.listeners[paramName].push(callback);
};
},
 
setter: function(opt){
if(opt.private[opt.paramName] == opt.newValue)
return;
 
opt.oldValue = Observable.clone(opt.private[opt.paramName]);
 
opt.private[opt.paramName] = opt.newValue;
 
for(var listener in opt.public.listeners[opt.paramName]){
if(opt.caller != opt.public.listeners[opt.paramName][listener])
opt.public.listeners[opt.paramName][listener](opt);
}
},
 
adder: function(opt){
if(opt.private[opt.paramName] == opt.newValue || !opt.private[opt.paramName].push)
return;
 
opt.oldValue = Observable.clone(opt.private[opt.paramName]);
 
opt.private[opt.paramName].push(opt.newValue);
opt.newValue = opt.private[opt.paramName];
 
for(var listener in opt.public.listeners[opt.paramName]){
if(opt.caller != opt.public.listeners[opt.paramName][listener])
opt.public.listeners[opt.paramName][listener](opt);
}
}
};
README.md
Markdown

MultiGetSet.JS 1.1.0

About

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"));

Try out

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​

Try out

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);

Try out

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​​

Try out

issues for next versions:

  • "add" method for array attributes
  • option to do it recursively inside private objects

add method was included, but I`ve changed my mind about the recursively option.

Here is a way to do that:

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!";

//this way you will erase the public data of the "private" object and add the required getters and setters
private = MultiGetSet({
    public: {},
    private: private,
    handler: Observable
  });

  MultiGetSet({
    public: public,
    private: private,
    handler: Observable
  });
};

both great functions !

I added two shortcuts to MultiGetSet for increment and decrement a variable as I use for a queue manager :

    opt.public.increment = function(paramName, add) {
      if (add == null) add = 1;
      opt.public.set(paramName, opt.public.get(paramName)+add);
    };

    opt.public.decrement = function(paramName, rem) {
      if (rem == null) rem = 1;
      opt.public.increment(paramName, -rem);
    };

@mistic100 great idea! This could be part of a plugin of Math operations

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.