|
10 | 10 | var gid = 0; |
11 | 11 | var noop = function() {}; |
12 | 12 |
|
| 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 | + |
13 | 41 | /** |
14 | 42 | * @namespace |
15 | 43 | * @name jBlocks |
|
78 | 106 | } catch (e) { |
79 | 107 | throw e; |
80 | 108 | } |
| 109 | + var Component = getComponentConstructor(name); |
81 | 110 | var instance = new Component(node, name, props); |
82 | 111 | var instanceId = instance.__id; |
83 | 112 |
|
|
117 | 146 | this.__id = ++gid; |
118 | 147 | this.__events = {}; |
119 | 148 | this.__handlerDomEvents = this.__handlerDomEvents.bind(this); |
120 | | - this.__bindMethods(); |
121 | 149 | this.__bindDomEvents(); |
122 | 150 |
|
123 | 151 | this.oninit(); |
|
199 | 227 | return null; |
200 | 228 | }, |
201 | 229 |
|
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 | | - |
221 | 230 | /** |
222 | 231 | * Bind DOM Events from decl |
223 | 232 | * @private |
|
0 commit comments