I released version 2.0 of occamsrazor.js. My goal is to further simplify the API. This example is taken from my last post:
//mediator
var pubsub = occamsrazor();
// from now a validator is a simple function
var is_archive = function (obj){
return 'getNumbers' in obj;
};
// the archive object
var archive = (function (){
var numbers = [];
return {
getNumbers: function (){
return numbers;
},
addItem: function (number){
numbers.push(number);
// this notify the event to the mediator
pubsub.publish('changed', this);
}
};
}());
// the printsum isn't changed
var printsum = (function (){
return {
print: function (n){
console.log(n);
},
sum_and_print: function (archive){
var i,sum = 0;
for(i = 0;i < archive.length; i++){
sum += archive[i];
}
this.print(sum);
}
};
}());
// subscribe the event
// subscribe is an alias of "add" and "on"
// the list of validators now is before the function
pubsub.subscribe(["changed",is_archive], function (evt, archive){
printsum.sum_and_print(archive.getNumbers());
});
//you can use a single string instead of a function as a validator