JSFiddle - React, Tailwind, and code Playground
by fatmatto
JavaScript
var Object = function(){
this.eventHandlers = [];
/*
* Default destructor
*
**/
this.destroy = function() {
this.emit('destroy');
delete this;
}
/*
*
* Emits a model event and calls its event handler if existing
* emit also accept a eventObject parameter that will be passed to the callback by on()
* If the eventObject contains a context, the callback is run within that context
*/
this.emit = function(eventName,eventObject) {
if (this.eventHandlers.hasOwnProperty(eventName)) {
this.eventHandlers[eventName].call(this,eventObject);
}
}
/**
* Binds an event to a callback (not DOM events, just nimble events)
*
**/
this.on = function(eventName,callback) { //Come faccio in modo che callback accetti argomenti?
if ('function' !== typeof callback)
throw new Error('Model.on() callback must be a function');
this.eventHandlers[eventName] = callback;
}
/**
* Use this setter if you want to trigger a "change" event
*
* Otherwise, just set manually a property
*
**/
this.set = function(prop,value) {
var oldval = this[prop];
this[prop] = value;
if(oldval !== this[prop])
this.emit('change '+prop);
}
/*
* This actually does nothing, it is here only to support a set/get coding style
*
*/
this.get = function(prop) {
return this[prop];
}
}
var collection = function() {
this.elements = [];
this.count = 0;
this.add = function(element) {
var position = this.elements.length;
...