Array.prototype.contains = function(pattern) {
return this.indexOf(pattern) > -1;
};
EventDispatcher = function(/* dispatcher */) {
var _listeners = [];
var _dispatcher = arguments[0] || this;
this.bindListener = function(event, listener) {
if(!(listener instanceof Object)) {
return;
}
if(!_listeners[event]) {
_listeners[event] = [];
}
_listeners[event].push(listener);
};
this.unbindListener = function(event, listener) {
if(_listeners[event] && _listeners[event].contains(listener)) {
_listeners[event].remove(listener);
}
};
this.dispatchEvent = function(event) {
if(!_listeners[event]) {
return;
}
for(var i = 0, length = this.listeners[event].length; i < length; i++) {
if(_listeners[event][i] instanceof Function) {
_listeners[event][i](event, _dispatcher);
}
}
};
};
/* пример использования */
Step = function(options) {
this.eventDispatcher = new EventDispatcher(this);
this.bindListener = function(event, listener) {
this.eventDispatcher.bindListener(event, listener);
};
this.unbindListener = function(event, listener) {
this.eventDispatcher.unbindListener(event, listener);
};
this.setValue = function(value) {
this._value = value;
this.eventDispatcher.dispatchEvent(Step.EVENT_SET_VALUE);
};
};
calc.Step.EVENT_SET_VALUE = 'setvalue';