Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added each function to collection
  • Loading branch information
gushov committed Dec 7, 2012
1 parent 0a6fe56 commit 844823d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -72,11 +72,11 @@ dunkel.save(function (err) {

### _model_.validate()

### _model_.save(callback)
### _model_.save(callback, context)

### _model_.fetch(callback)
### _model_.fetch(callback, context)

### _model_.destroy(callback)
### _model_.destroy(callback, context)

### collection.extend(config)

Expand All @@ -90,7 +90,9 @@ dunkel.save(function (err) {

### _collection_.get(query)

### _collection_.find(query, callback)
### _collection_.each(callback, context)

### _collection_.find(callback, context)

## License
Copyright (c) 2012 August Hovland
Expand Down
30 changes: 20 additions & 10 deletions dist/lilmodel.js
@@ -1,4 +1,4 @@
/*! lilmodel - v0.0.2 - 2012-12-06
/*! lilmodel - v0.0.3 - 2012-12-07
* Copyright (c) 2012 August Hovland <gushov@gmail.com>; Licensed MIT */

(function (ctx) {
Expand Down Expand Up @@ -305,6 +305,10 @@ var validator = {
return typeof value === 'string';
},

boolean: function (value) {
return typeof value === 'boolean';
},

length: function (value, min, max) {

var isBigEnough = !min || value.length >= min;
Expand Down Expand Up @@ -427,9 +431,15 @@ module.exports = LilObj.extend({

},

find: function (query, next) {
each: function (next, ctx) {
_.each(this.$, next, ctx);
},

find: function (next, ctx) {

var sync = syncr();
sync('find', this, next);
sync('find', this, next.bind(ctx));

}

});
Expand Down Expand Up @@ -518,28 +528,28 @@ module.exports = LilObj.extend({

},

save: function (next) {
save: function (next, ctx) {

var sync = syncr();
var method = this.$._id ? 'update' : 'create';
var validation = this.validate();

if (validation.isValid) {
sync(method, this, next);
sync(method, this, next.bind(ctx));
} else {
next(validation.error, this);
next.call(ctx, validation.error, this);
}

},

fetch: function (next) {
fetch: function (next, ctx) {
var sync = syncr();
sync('fetch', this, next);
sync('fetch', this, next.bind(ctx));
},

destroy: function (next) {
destroy: function (next, ctx) {
var sync = syncr();
sync('delete', this, next);
sync('delete', this, next.bind(ctx));
}

});
Expand Down
4 changes: 2 additions & 2 deletions dist/lilmodel.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions lib/lilmodel/collection.js
Expand Up @@ -64,9 +64,15 @@ module.exports = LilObj.extend({

},

find: function (query, next) {
each: function (next, ctx) {
_.each(this.$, next, ctx);
},

find: function (next, ctx) {

var sync = syncr();
sync('find', this, next);
sync('find', this, next.bind(ctx));

}

});
14 changes: 7 additions & 7 deletions lib/lilmodel/model.js
Expand Up @@ -78,28 +78,28 @@ module.exports = LilObj.extend({

},

save: function (next) {
save: function (next, ctx) {

var sync = syncr();
var method = this.$._id ? 'update' : 'create';
var validation = this.validate();

if (validation.isValid) {
sync(method, this, next);
sync(method, this, next.bind(ctx));
} else {
next(validation.error, this);
next.call(ctx, validation.error, this);
}

},

fetch: function (next) {
fetch: function (next, ctx) {
var sync = syncr();
sync('fetch', this, next);
sync('fetch', this, next.bind(ctx));
},

destroy: function (next) {
destroy: function (next, ctx) {
var sync = syncr();
sync('delete', this, next);
sync('delete', this, next.bind(ctx));
}

});
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "August Hovland <gushov@gmail.com>",
"name": "lilmodel",
"description": "A li'l model wrapper",
"version": "0.0.3",
"version": "0.0.4",
"repository": {
"type": "git",
"url": "git://github.com/gushov/lilmodel.git"
Expand Down
5 changes: 4 additions & 1 deletion test/lilmodel-test.js
Expand Up @@ -69,9 +69,12 @@ buster.testCase("lilmodel", {
var results = chef.recipes.get({ name: 'tacos' });
chef.recipes.remove({ name: 'meatball sauce' });

chef.save(nextSpy);

var context = { me: 'gus' };
chef.save(nextSpy, context);
assert.calledOnce(this.syncStub);
assert.calledOnce(nextSpy);
assert(nextSpy.calledOn(context));
assert.equals(chef.name, 'gus');
assert.equals(chef.sousChef.name, 'zoe');
assert.equals(chef.recipes.$[0].name, 'tacos');
Expand Down

0 comments on commit 844823d

Please sign in to comment.