Skip to content

Commit 2c04f0a

Browse files
committed
Merge pull request #19 from vitkarpov/fix-#18
[#18] implement multiple inheritance for component
2 parents 1580726 + 138e7b1 commit 2c04f0a

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
2-
before_script: npm run-script build
32
node_js:
43
- 0.10
54
notifications:

lib/index.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
1010
var gid = 0;
1111
var noop = function() {};
1212

13+
/**
14+
* Returns constuctor for component with the given name.
15+
* Implement the right chain of prototypes:
16+
* instance of the component -> methods from decl -> base component methods
17+
* @param {String} name
18+
* @return {Function}
19+
*/
20+
var getComponentConstructor = function(name) {
21+
var F = function() {
22+
Component.apply(this, arguments);
23+
};
24+
var decl = declarations[name] || {};
25+
var methods = decl.methods || {};
26+
27+
methods.oninit = methods.oninit || noop;
28+
methods.ondestroy = methods.ondestroy || noop;
29+
30+
F.prototype = Object.create(Component.prototype);
31+
F.prototype.constuctor = Component;
32+
33+
for (var method in methods) {
34+
if (methods.hasOwnProperty(method)) {
35+
F.prototype[method] = methods[method];
36+
}
37+
}
38+
return F;
39+
};
40+
1341
/**
1442
* @namespace
1543
* @name jBlocks
@@ -78,6 +106,7 @@
78106
} catch (e) {
79107
throw e;
80108
}
109+
var Component = getComponentConstructor(name);
81110
var instance = new Component(node, name, props);
82111
var instanceId = instance.__id;
83112

@@ -117,7 +146,6 @@
117146
this.__id = ++gid;
118147
this.__events = {};
119148
this.__handlerDomEvents = this.__handlerDomEvents.bind(this);
120-
this.__bindMethods();
121149
this.__bindDomEvents();
122150

123151
this.oninit();
@@ -199,25 +227,6 @@
199227
return null;
200228
},
201229

202-
/**
203-
* Bind methods from decl to the instance
204-
* @private
205-
* @return {jBlocks.Component}
206-
*/
207-
__bindMethods: function() {
208-
var methods = this.__decl.methods || {};
209-
210-
methods.oninit = methods.oninit || noop;
211-
methods.ondestroy = methods.ondestroy || noop;
212-
213-
for (var name in methods) {
214-
if (methods.hasOwnProperty(name)) {
215-
this[name] = methods[name];
216-
}
217-
}
218-
return this;
219-
},
220-
221230
/**
222231
* Bind DOM Events from decl
223232
* @private

tests/specs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ describe('jblocks', function() {
6969

7070
instance.name.should.eql('bar');
7171
});
72+
it('should return an instance of jBlocks.Component', function() {
73+
var instance = jBlocks.get(document.querySelector('.js-bar'));
74+
75+
(instance instanceof jBlocks.Component).should.eql(true);
76+
});
7277
});
7378
describe('#destroy', function() {
7479
beforeEach(function() {

0 commit comments

Comments
 (0)