[imp] added most methods for EventDispatcherMixin
bzr revid: nicolas.vanhoren@openerp.com-20120223173959-di5xl97uci1pnz6x
This commit is contained in:
parent
0246ffeee0
commit
334d3f5875
|
@ -248,20 +248,47 @@ niv = (function() {
|
|||
};
|
||||
lib.EventDispatcherMixin = _.extend({}, lib.ParentedMixin, {
|
||||
__eventDispatcherMixin: true,
|
||||
bind: function(eventName, object, func) {
|
||||
bind: function(events, object, func) {
|
||||
var self = this;
|
||||
checkEventDispatcher(this);
|
||||
this.__events.on(eventName, func, object);
|
||||
if (object && object.__eventDispatcherMixin) {
|
||||
checkEventDispatcher(object);
|
||||
object.__registeredEvents.push({name: eventName, func: func, source: this});
|
||||
}
|
||||
events = events.split(/\s+/);
|
||||
_.each(events, function(eventName) {
|
||||
self.__events.on(eventName, func, object);
|
||||
if (object && object.__eventDispatcherMixin) {
|
||||
checkEventDispatcher(object);
|
||||
object.__registeredEvents.push({name: eventName, func: func, source: self});
|
||||
}
|
||||
});
|
||||
return this;
|
||||
},
|
||||
unbind: function(events, object, func) {
|
||||
var self = this;
|
||||
checkEventDispatcher(this);
|
||||
events = events.split(/\s+/);
|
||||
_.each(events, function(eventName) {
|
||||
self.__events.off(eventName, func, object);
|
||||
if (object && object.__eventDispatcherMixin) {
|
||||
checkEventDispatcher(object);
|
||||
object.__registeredEvents = _.filter(object.__registeredEvents, function(el) {
|
||||
return !(el.name === eventName && el.func === func && el.source === self);
|
||||
});
|
||||
}
|
||||
});
|
||||
return this;
|
||||
},
|
||||
trigger: function(events) {
|
||||
checkEventDispatcher(this);
|
||||
this.__events.trigger.apply(this.__events, arguments);
|
||||
return this;
|
||||
},
|
||||
destroy: function() {
|
||||
var self = this;
|
||||
checkEventDispatcher(this);
|
||||
_.each(this.__registeredEvents, _.bind(function(event) {
|
||||
event.source.__events.off(event.name, event.func, this);
|
||||
}, this));
|
||||
this.__events.off(); // to ease garbage collector's duty
|
||||
_.each(this.__registeredEvents, function(event) {
|
||||
event.source.__events.off(event.name, event.func, self);
|
||||
});
|
||||
this.__registeredEvents = [];
|
||||
this.__events.off();
|
||||
lib.ParentedMixin.destroy.call(this);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
|
||||
module("Class");
|
||||
|
||||
test("Class exists", function() {
|
||||
ok(!!niv.Class, "Class does not exist");
|
||||
ok(!!niv.Class.extend, "extend does not exist");
|
||||
});
|
||||
|
||||
test("Inheritance works", function() {
|
||||
test("base", function() {
|
||||
ok(!!niv.Class, "Class does not exist");
|
||||
ok(!!niv.Class.extend, "extend does not exist");
|
||||
var Claz = niv.Class.extend({
|
||||
test: function() {
|
||||
return "ok";
|
||||
|
@ -17,7 +14,7 @@ test("Inheritance works", function() {
|
|||
|
||||
module("DestroyableMixin");
|
||||
|
||||
test("DestroyableMixin works", function() {
|
||||
test("base", function() {
|
||||
var Claz = niv.Class.extend(_.extend({}, niv.DestroyableMixin, {}));
|
||||
var x = new Claz();
|
||||
equal(false, !!x.isDestroyed());
|
||||
|
@ -27,7 +24,7 @@ test("DestroyableMixin works", function() {
|
|||
|
||||
module("ParentedMixin");
|
||||
|
||||
test("ParentedMixin works", function() {
|
||||
test("base", function() {
|
||||
var Claz = niv.Class.extend(_.extend({}, niv.ParentedMixin, {}));
|
||||
var x = new Claz();
|
||||
var y = new Claz();
|
||||
|
@ -40,7 +37,7 @@ test("ParentedMixin works", function() {
|
|||
|
||||
module("Events");
|
||||
|
||||
test("Events works", function() {
|
||||
test("base", function() {
|
||||
var x = new niv.internal.Events();
|
||||
var tmp = 0;
|
||||
var fct = function() {tmp = 1;};
|
||||
|
@ -53,3 +50,26 @@ test("Events works", function() {
|
|||
x.trigger("test");
|
||||
equal(0, tmp);
|
||||
});
|
||||
|
||||
module("EventDispatcherMixin");
|
||||
|
||||
test("base", function() {
|
||||
var Claz = niv.Class.extend(_.extend({}, niv.EventDispatcherMixin, {}));
|
||||
var x = new Claz();
|
||||
var y = new Claz();
|
||||
var tmp = 0;
|
||||
var fct = function() {tmp = 1;};
|
||||
x.bind("test", y, fct);
|
||||
equal(0, tmp);
|
||||
x.trigger("test");
|
||||
equal(1, tmp);
|
||||
tmp = 0;
|
||||
x.unbind("test", y, fct);
|
||||
x.trigger("test");
|
||||
equal(0, tmp);
|
||||
tmp = 0;
|
||||
x.bind("test", y, fct);
|
||||
y.destroy();
|
||||
x.trigger("test");
|
||||
equal(0, tmp);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue