Skip to content

Commit

Permalink
updated collection find api
Browse files Browse the repository at this point in the history
  • Loading branch information
gushov committed Dec 10, 2012
1 parent 3f700fc commit 6d04925
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 22 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ dunkel.save(function (err) {

### _collection_.validate()

### _collection_.add(model)
### _collection_.add(properties)

### _collection_.remove(query)

### _collection_.get(query)

### _collection_.each(callback, context)

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

## License
Copyright (c) 2012 August Hovland
Expand Down
31 changes: 23 additions & 8 deletions dist/lilmodel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! lilmodel - v0.0.5 - 2012-12-09
/*! lilmodel - v0.0.6 - 2012-12-10
* Copyright (c) 2012 August Hovland <gushov@gmail.com>; Licensed MIT */

(function (ctx) {
Expand Down Expand Up @@ -407,6 +407,17 @@ var arr = require('lilobj').arr;
var _ = require('lil_');
var syncr = require('./syncr');

function parser(ctx, model, next) {

return function (err, values) {

var instance = !err && model.create(values);
next.call(ctx, err, instance);

};

}

module.exports = arr.extend({

construct: function (values) {
Expand Down Expand Up @@ -473,14 +484,11 @@ module.exports = arr.extend({

},

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

find: function (next, ctx) {
find: function (query, next, ctx) {

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

}

Expand Down Expand Up @@ -520,7 +528,10 @@ function setter(name, value) {
function parser(ctx, model, next) {

return function (err, values) {
next.call(ctx, err, model.create(values || {}));

var instance = !err ? model.create(values) : null;
next.call(ctx, err, instance);

};

}
Expand Down Expand Up @@ -593,13 +604,17 @@ module.exports = obj.extend({
},

fetch: function (next, ctx) {

var sync = syncr();
sync('fetch', this, parser(ctx, this, next));

},

destroy: function (next, ctx) {

var sync = syncr();
sync('destroy', this, parser(ctx, this, next));

}

});
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.

12 changes: 6 additions & 6 deletions lib/lilmodel/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ var syncr = require('./syncr');
function parser(ctx, model, next) {

return function (err, values) {
next.call(ctx, err, model.create(values || []));

var instance = !err && model.create(values);
next.call(ctx, err, instance);

};

}
Expand Down Expand Up @@ -80,13 +83,10 @@ module.exports = arr.extend({

},

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

find: function (next, ctx) {
find: function (query, next, ctx) {

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

}
Expand Down
9 changes: 8 additions & 1 deletion lib/lilmodel/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ function setter(name, value) {
function parser(ctx, model, next) {

return function (err, values) {
next.call(ctx, err, model.create(values));

var instance = !err ? model.create(values) : null;
next.call(ctx, err, instance);

};

}
Expand Down Expand Up @@ -101,13 +104,17 @@ module.exports = obj.extend({
},

fetch: function (next, ctx) {

var sync = syncr();
sync('fetch', this, parser(ctx, this, next));

},

destroy: function (next, ctx) {

var sync = syncr();
sync('destroy', this, parser(ctx, this, next));

}

});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "August Hovland <gushov@gmail.com>",
"name": "lilmodel",
"description": "A li'l model wrapper",
"version": "0.0.6",
"version": "0.0.7",
"repository": {
"type": "git",
"url": "git://github.com/gushov/lilmodel.git"
Expand Down
30 changes: 30 additions & 0 deletions test/lilmodel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ buster.testCase("lilmodel", {
assert.equals(results.length, 1);
assert.equals(results[0], chef.recipes[0]);

},

"should call sync on collection find": function () {

var syncStub = this.stub().callsArg(2);
var nextSpy = this.spy();

lilModel.syncr(syncStub);

var Recipe = lilModel.model.extend({
rules: {
name: ['required', 'string']
}
});

var Recipes = lilModel.collection.extend({
model: Recipe
});

var recipes = Recipes.create();
var context = { me: 'gus' };
var query = { name: 'gus' };
recipes.find(query, nextSpy, context);

assert(syncStub.calledOnce);
assert(syncStub.calledWith('find', recipes));
assert.equals(syncStub.getCall(0).args[1].query, query);
assert(nextSpy.calledOnce);
assert(nextSpy.calledOn(context));

}

});

0 comments on commit 6d04925

Please sign in to comment.