Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More eventing and docs

  • Loading branch information...
commit 1eca906cb00251c20ef6eb770db0f0a30f570794 1 parent d460d03
mde authored
Showing with 84 additions and 3 deletions.
  1. +31 −0 README.md
  2. +10 −3 lib/index.js
  3. +43 −0 test/events.js
31 README.md
View
@@ -152,6 +152,37 @@ if (user.isValid()) {
}
```
+## Lifecycle events
+
+Both the base model 'constructors,' and model instances are EventEmitters. The
+emit events during the create/update/remove lifecycle of model instances. In all
+cases, the plain-named event is fired after the event in question, the
+'before'-prefixed event, of course happens before.
+
+The 'constructor' for a model emits the following events:
+
+ * beforeCreate
+ * create
+ * beforeValidate
+ * validate
+ * beforeUpdateProperties
+ * updateProperties
+ * beforeSave (new instances, single and bulk)
+ * save (new instances, single and bulk)
+ * beforeUpdate (existing single instances, bulk updates)
+ * update (existing single instances, bulk updates)
+ * beforeRemove
+ * remove
+
+Model-item instances emit these events:
+
+ * beforeUpdateProperties
+ * updateProperties
+ * beforeSave
+ * save
+ * beforeUpdate
+ * update
+
## Querying
Model uses a simple API for finding and sorting items. Again, it should look
13 lib/index.js
View
@@ -314,9 +314,6 @@ utils.mixin(model, new (function () {
*/
obj.create = function () {
var args = Array.prototype.slice.call(arguments);
- args.unshift('beforeCreate');
- this.emit.apply(this, args);
- args.shift();
args.unshift(name);
return model.createItem.apply(model, args);
};
@@ -560,6 +557,9 @@ utils.mixin(model, new (function () {
var params = p || {}
, opts = o || {}
, item = new model[name](params);
+
+ model[name].emit('beforeCreate', p, o);
+
item = this.validateAndUpdateFromParams(item, params, opts);
if (this.useTimestamps && !item.createdAt) {
@@ -578,6 +578,9 @@ utils.mixin(model, new (function () {
var data = {}
, name = item.type;
+ model[name].emit('beforeUpdateProperties', item, params, opts);
+ item.emit('beforeUpdateProperties');
+
utils.mixin(data, item);
utils.mixin(data, params);
this.validateAndUpdateFromParams(item, data, opts);
@@ -586,6 +589,10 @@ utils.mixin(model, new (function () {
if (typeof item.afterUpdate === 'function') {
item.afterUpdate();
}
+
+ model[name].emit('updateProperties', item);
+ item.emit('updateProperties');
+
return item;
};
43 test/events.js
View
@@ -63,6 +63,49 @@ tests = {
var user = User.create(_params);
}
+, 'emit static beforeUpdateProperties': function (next) {
+ User.once('beforeUpdateProperties', function (u, p) {
+ assert.ok(u instanceof User);
+ assert.equal('zzz', u.login);
+ assert.equal('yyz', p.login);
+ next();
+ });
+ var user = User.create(_params);
+ user.updateProperties({login: 'yyz'});
+ }
+
+, 'emit instance beforeUpdateProperties': function (next) {
+ var user = User.create(_params);
+ user.once('beforeUpdateProperties', function () {
+ assert.equal('zzz', user.login);
+ next();
+ });
+ user.save(function () {
+ user.updateProperties({login: 'yyz'});
+ });
+ }
+
+, 'emit static updateProperties': function (next) {
+ User.once('updateProperties', function (u) {
+ assert.ok(u instanceof User);
+ assert.equal('yyz', u.login);
+ next();
+ });
+ var user = User.create(_params);
+ user.updateProperties({login: 'yyz'});
+ }
+
+, 'emit instance updateProperties': function (next) {
+ var user = User.create(_params);
+ user.once('updateProperties', function () {
+ assert.equal('yyz', user.login);
+ next();
+ });
+ user.save(function () {
+ user.updateProperties({login: 'yyz'});
+ });
+ }
+
, 'emit static beforeSave': function (next) {
User.once('beforeSave', function (u) {
assert.ok(u instanceof User);
Please sign in to comment.
Something went wrong with that request. Please try again.