promised-models
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -12,2 +12,33 @@ /** | ||
/** | ||
* bind events on attributes or model | ||
* @param {EventEmitter} eventEmitter | ||
* @param {string} method of EventEmitter | ||
* @param {string} [attribute] space separated list | ||
* @param {string} event space separated list | ||
* @param {Function} cb | ||
* @param {*} [ctx] | ||
*/ | ||
callEventEmitter = function (eventEmitter, method, attribute, event, cb, ctx) { | ||
if (typeof event !== 'string') { | ||
ctx = cb; | ||
cb = event; | ||
event = attribute; | ||
attribute = undefined; | ||
} | ||
ctx = ctx || this; | ||
if (attribute) { | ||
attribute.split(/\s+/).forEach(function (attribute) { | ||
event.split(/\s+/).forEach(function (event) { | ||
eventEmitter[method](event + ':' + attribute, cb, ctx); | ||
}); | ||
}); | ||
} else { | ||
event.split(/\s+/).forEach(function (event) { | ||
eventEmitter[method](event, cb, ctx); | ||
}); | ||
} | ||
}, | ||
/** | ||
* @class | ||
@@ -301,3 +332,7 @@ * @prop {object} [data] initial data | ||
trigger: function () { | ||
var callEmitter = this.__self._eventEmitter; | ||
this._eventEmitter.emit.apply(this._eventEmitter, arguments); | ||
if (callEmitter) { | ||
callEmitter.emit.apply(callEmitter, arguments); | ||
} | ||
}, | ||
@@ -473,21 +508,3 @@ | ||
_callEventEmitter: function (method, attribute, event, cb, ctx) { | ||
var model = this; | ||
if (typeof event !== 'string') { | ||
ctx = cb; | ||
cb = event; | ||
event = attribute; | ||
attribute = undefined; | ||
} | ||
ctx = ctx || this; | ||
if (attribute) { | ||
attribute.split(/\s+/).forEach(function (attribute) { | ||
event.split(/\s+/).forEach(function (event) { | ||
model._eventEmitter[method](event + ':' + attribute, cb, ctx); | ||
}); | ||
}); | ||
} else { | ||
event.split(/\s+/).forEach(function (event) { | ||
model._eventEmitter[method](event, cb, ctx); | ||
}); | ||
} | ||
callEventEmitter(this._eventEmitter, method, attribute, event, cb, ctx); | ||
}, | ||
@@ -536,2 +553,31 @@ | ||
/** | ||
* bind events for models of class | ||
* @param {string} [attribute] space separated list | ||
* @param {string} event space separated list | ||
* @param {Function} cb | ||
* @param {*} [ctx] | ||
* @return {Model} | ||
*/ | ||
on: function (attribute, event, cb, ctx) { | ||
this._eventEmitter = this._eventEmitter || new EventEmitter(); | ||
callEventEmitter(this._eventEmitter, 'on', attribute, event, cb, ctx); | ||
return this; | ||
}, | ||
/** | ||
* unbind events from models of class | ||
* @param {string} [attribute] space separated list | ||
* @param {string} event space separated list | ||
* @param {Function} cb | ||
* @param {*} [ctx] | ||
* @return {Model} | ||
*/ | ||
un: function (attribute, event, cb, ctx) { | ||
if (this._eventEmitter) { | ||
callEventEmitter(this._eventEmitter, 'removeListener', attribute, event, cb, ctx); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* @class | ||
@@ -538,0 +584,0 @@ * @abstract |
{ | ||
"description": "promise based, typed attributes, nested models and collections", | ||
"name": "promised-models", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"repository": "git@github.com:delfrrr/promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -458,2 +458,15 @@ # Promised Models | ||
#### `Model.on([attributes], events, cb, [ctx])` | ||
Bind event on all models of class | ||
```js | ||
FashionModel.on('change', this.changeHandler, this); | ||
``` | ||
#### `Model.un([attributes], events, cb, [ctx])` | ||
Unbind event on all models of class | ||
### List | ||
@@ -460,0 +473,0 @@ |
@@ -6,3 +6,27 @@ | ||
var ModelClass = require('./models/simple'); | ||
describe('Models.on', function () { | ||
describe('Model.un', function () { | ||
it('should unsubscribe from event', function (done) { | ||
var model = new ModelClass(), | ||
cb = function () { | ||
model.un('change', cb); | ||
model.set('a', 'a2'); | ||
done(); | ||
}; | ||
model.on('change', cb); | ||
model.set('a', 'a1'); | ||
}); | ||
}); | ||
describe('Model.un', function () { | ||
it('should bind on events', function (done) { | ||
var model = new ModelClass(), | ||
cb = function () { | ||
ModelClass.un('change', cb); | ||
done(); | ||
}; | ||
ModelClass.on('change', cb); | ||
model.set('a', 'a1'); | ||
}); | ||
}); | ||
describe('model.on', function () { | ||
it('should bind on few events', function (done) { | ||
@@ -37,3 +61,3 @@ var model = new ModelClass(), | ||
}); | ||
describe('Models.un', function () { | ||
describe('model.un', function () { | ||
var model, count; | ||
@@ -40,0 +64,0 @@ beforeEach(function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112752
2902
524