Comparing version
@@ -8,3 +8,3 @@ 'use strict' | ||
const shift = Array.prototype.shift | ||
const _merge = forge._merge | ||
const merge = forge.merge | ||
@@ -33,3 +33,3 @@ // PUBLIC | ||
_merge ( this, comp.methods ) | ||
merge ( this, comp.methods ) | ||
@@ -73,13 +73,20 @@ // Do not call setup or components.push more then once | ||
{ let method = this [ key ] | ||
if ( !method ) | ||
{ throw new Error | ||
( `Cannot set '${key}' (no method with this name).` ) | ||
if ( typeof method == 'function' && !method._user_method ) | ||
{ let args = definition [ key ] | ||
if ( args instanceof Array ) | ||
{ method.apply ( this, args ) | ||
} | ||
else | ||
{ method.call ( this, args ) | ||
} | ||
} | ||
let args = definition [ key ] | ||
if ( args instanceof Array ) | ||
{ method.apply ( this, args ) | ||
} | ||
else | ||
{ method.call ( this, args ) | ||
{ let value = definition [ key ] | ||
if ( typeof value == 'function' ) | ||
{ value._user_method = true | ||
this [ key ] = value | ||
} | ||
else | ||
{ this [ key ] = value | ||
} | ||
} | ||
@@ -140,2 +147,3 @@ } | ||
{ callbacksByEvent [ event ] = callback | ||
callback.idx = 0 | ||
} | ||
@@ -145,5 +153,7 @@ else if ( typeof callbacks === 'function' ) | ||
{ callbacksByEvent [ event ] = callback | ||
callback.idx = 0 | ||
} | ||
else | ||
{ callbacksByEvent [ event ] = [ callbacks, callback ] | ||
callback.idx = 1 | ||
} | ||
@@ -153,4 +163,4 @@ } | ||
{ if ( replacedCallback ) | ||
{ let i = callbacks.indexOf ( replacedCallback ) | ||
if ( i < 0 ) | ||
{ let i = replacedCallback.idx | ||
if ( i == undefined ) | ||
{ throw new Error | ||
@@ -160,5 +170,7 @@ ( `Invalid function passed to replace callback.` ) | ||
callbacks [ i ] = callback | ||
callback.idx = i | ||
} | ||
else | ||
{ callbacks.push ( callback ) | ||
callback.idx = callbacks.length | ||
} | ||
@@ -165,0 +177,0 @@ } |
@@ -0,1 +1,9 @@ | ||
== 0.5.0 2015-09-22 | ||
* Enable binding reuse. | ||
* Entity#set on missing method should set value. | ||
* Entity#set detects user defined methods from component methods. | ||
* forge#merge method now public. | ||
* compatible with latest node.js (ES6 features). | ||
== 0.4.0 2015-09-09 | ||
@@ -2,0 +10,0 @@ |
21
index.js
@@ -147,3 +147,3 @@ /* | ||
const _merge = function ( target, source ) | ||
const merge = function ( target, source ) | ||
{ for ( let key in source ) | ||
@@ -155,4 +155,15 @@ { if ( source.hasOwnProperty ( key ) ) | ||
} | ||
lib._merge = _merge | ||
const setParam = function ( self, key, def, defvalue ) | ||
{ if ( def.hasOwnProperty ( key ) ) | ||
{ self [ key ] = def [ key ] | ||
} | ||
else if ( ! self.hasOwnProperty ( key ) ) | ||
{ self [ key ] = defvalue | ||
} | ||
} | ||
lib.merge = merge | ||
lib.setParam = setParam | ||
/////////////////////////////// Public | ||
@@ -211,7 +222,7 @@ | ||
else | ||
{ _merge ( self, classMethods ) | ||
{ merge ( self, classMethods ) | ||
} | ||
let methods = self.methods | ||
_merge ( methods, definition ) | ||
merge ( methods, definition ) | ||
@@ -228,3 +239,3 @@ if ( isNew ) | ||
for ( let i = 0, len = entities.length; i < len; i++ ) | ||
{ _merge ( entities [ i ], methods ) | ||
{ merge ( entities [ i ], methods ) | ||
} | ||
@@ -231,0 +242,0 @@ } |
{ | ||
"name": "lucy-forge", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Entity and component definition tool", |
@@ -15,4 +15,2 @@ # Lucy forge [](https://travis-ci.org/lucidogen/lucy-forge) | ||
Currently only works with [**io.js**](https://iojs.org). | ||
```shell | ||
@@ -102,2 +100,3 @@ npm install lucy-forge --save | ||
* 0.5.0 (2015-09-22) Better #set method, forge.merge. | ||
* 0.4.0 (2015-09-09) Renaming init to 'setup'. Init is now called on class. | ||
@@ -104,0 +103,0 @@ * 0.3.0 (2015-09-04) API change for forge.Component with class methods. |
@@ -444,2 +444,47 @@ 'use strict' | ||
it | ||
( 'should set elements without methods on #set' | ||
, function () | ||
{ e = forge.Entity | ||
( 'Person' | ||
, { foo: 'barfoo' | ||
} | ||
) | ||
e.foo | ||
.should.equal ( 'barfoo' ) | ||
e.set ( { foo: 'bal' } ) | ||
e.foo | ||
.should.equal ( 'bal' ) | ||
} | ||
) | ||
it | ||
( 'should set method with #set' | ||
, function () | ||
{ e = forge.Entity | ||
( 'Person' | ||
, { foo ( m ) | ||
{ if ( m ) | ||
throw new Error ('Should not call when replacing function' ) | ||
return 'barfoo' | ||
} | ||
} | ||
) | ||
e.foo () | ||
.should.equal ( 'barfoo' ) | ||
e.set | ||
( { foo () { return 'bal' } | ||
} | ||
) | ||
e.foo () | ||
.should.equal ( 'bal' ) | ||
} | ||
) | ||
it | ||
( 'should return this in #set' | ||
@@ -495,4 +540,17 @@ , function () | ||
.should.be.a ( 'function' ) | ||
b.idx | ||
.should.equal ( 0 ) | ||
b = e.bind | ||
let c = e.bind | ||
( 'Hit' | ||
, function () | ||
{ this._test3 = 'Hit3' | ||
} | ||
) | ||
c.idx | ||
.should.equal ( 1 ) | ||
e.bind | ||
( b | ||
@@ -508,2 +566,4 @@ , function () | ||
.should.equal ( 'Hit2 Test' ) | ||
e._test3 | ||
.should.equal ( 'Hit3' ) | ||
} | ||
@@ -510,0 +570,0 @@ ) |
Sorry, the diff of this file is not supported yet
30811
7.28%948
8.1%104
-0.95%