Base class and events manager
$ npm install hr.class
Create a new class by extending the default Class
:
var Class = require("hr.class");
var MyClass = Class.extend({
// Default options
defaults: {
},
// Constructor
initialize: function() {
}
});
Class instances can emit events:
obj.trigger("myevent");
// obj.emit("myevent") is equivalent
// Sub-events
obj.trigger("myevent:subevent")
// With event property
obj.trigger("myevent", {
myproperty: 1
});
Listening to event using .on
:
obj.on("myevent", function(e) {
// Will be called for "myevent" and "myevent:subevent"
});
listenTo
can also be used to avoid memeory leaks in events listeners when an object is listening to another object:
obj1.listenTo(obj2, "myevent", function() {
// this == obj1
});
Class can be extended using mixins.
var MyMixin = Class.Mixin({
myMethod: function() {
...
}
});
var ChildClass = MyClass.mixim(
MyMixin,
AnotherMixin
)
.extend({
});