Arale
This is a fork of arale.class and arale.events. The core of arale projects.
The Class and Events of Arale is very easy to use, and stable now. Test cases with 97% coverage, and you don't have to worry about the 3% left.
Class
Read it in Chinese at Arale.Class.
create
Class.create([parent], [properties])
create a new class:
var Class = require('arale').Class;
var Pig = Class.create({
initialize: function(name) {
this.name = name;
},
talk: function() {
console.log('I am' + this.name);
}
});
module.exports = Pig;
extend
Every class created by Class.create
has an extend
method:
var Pig = require('./pig');
var RedPig = Pig.extend({
initialize: function(name) {
RedPig.superclass.initialize.call(this, name);
},
color: 'red'
});
module.exports = RedPig;
implement
You can also mixin other classes:
exports.fly = function() {
console.log('I am flying');
};
var RedPig = require('./red-pig');
var Flyable = require('./flyable');
var FlyableRedPig = RedPig.extend({
Implements: Flyable,
initialize: function(name) {
FlyableRedPig.superclass.initialize.call(this, name);
}
});
module.exports = FlyableRedPig;
The other way of mixin:
var FlyableRedPig = require('./flyable-red-pig');
FlyableRedPig.implement({
swim: function() {
console.log('I can swim');
}
});
Class
Transfrom a function to class:
function Animal() {
}
Animal.prototype.talk = function() {};
var Dog = Class(Animal).extend({
swim: function() {}
});
Events
Read it in Chinese at Arale.Events.
How to
There are two ways of implementing events:
var Events = require('arale').Events;
var object = new Events();
object.on('expand', function() {
console.log('expanded');
});
object.trigger('expand');
The other way:
var Events = require('arale').Events;
function Dog() {
}
Events.mixTo(Dog);
Dog.prototype.sleep = function() {
this.trigger('sleep');
};
var dog = new Dog();
dog.on('sleep', function() {
console.log('the dog is sleeping');
});
dog.sleep();
on
object.on(event, callback, [context])
You can pass a context to change the this.
post.on('saved', callback, that);
There is an all
event:
proxy.on('all', function(eventName) {
object.trigger(eventName);
});
off
object.off([event], [callback], [context])
object.off('change', onChange);
object.off('change');
object.off(null, onChange);
object.off(null, null, context);
object.off();
trigger
object.trigger(event, [*args])
var obj = new Events();
obj.on('x y', fn);
obj.on('x').on('y');