Closed alexandrubau closed 8 years ago
Let me check, I will get back to you a.s.a.p.
I ended up making a mixin like so:
Blend.defineClass('App.mixin.Observable', {
/**
* Maps to event emitter
*/
on: function(){
if( !this.event ){
var EventEmitter = require('events').EventEmitter;
this.event = new EventEmitter;
}
this.event.on.apply(this.event, arguments);
},
/**
* Maps to event emitter
*/
emit: function(){
if( !this.event ){
var EventEmitter = require('events').EventEmitter;
this.event = new EventEmitter;
}
this.event.emit.apply(this.event, arguments);
}
});
You think it's a good approach? What I don't like is that I have to duplicate each method (on, emit, addListener, etc.).
I was about the suggest the same approach :) At the moment the ClassBuilder cannot "mix" non-blend classes. ClassBuilder.js:338.
On the side track, your requirement gave me the idea to create a Dependency Injection Container :)
Nice. Thanks for your time.
I've also noticed that the init()
method is not called for mixins. Is this the intended behaviour?
It would be nice to have it called, to place some initialization logic, this way avoiding the duplicate code.
if( !this.event ){
var EventEmitter = require('events').EventEmitter;
this.event = new EventEmitter;
}
That behavior is intentional because, the class-system will not be able to decide "when" to and in which order to call the mixin's init
method. Imagine you have multiple mixins and one depends on the other.
Is it possbile to use a Blend Class to inherit methods from Node.js's EventEmitter module?
Thanks