From 4bcfe2b7e80e9854649390d592d93b77152309bd Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Wed, 19 Dec 2012 15:20:22 -0600 Subject: [PATCH 01/20] added async plugin support --- lib/proto.js | 83 +++++++++++++++++++++++++++++++++++---------------- test/model.js | 24 +++++++++++++-- 2 files changed, 78 insertions(+), 29 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index e351684..51380c5 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -129,14 +129,14 @@ exports.remove = function(fn){ if (this.isNew()) return fn(new Error('not saved')); var self = this; var url = this.url(); - this.model.emit('removing', this); - this.emit('removing'); - request.del(url, function(res){ - if (res.error) return fn(error(res)); - self.removed = true; - self.model.emit('remove', self); - self.emit('remove'); - fn(); + this.run('removing', function() { + request.del(url, function(res){ + if (res.error) return fn(error(res)); + self.removed = true; + self.model.emit('remove', self); + self.emit('remove'); + fn(); + }); }); }; @@ -158,15 +158,15 @@ exports.save = function(fn){ var url = this.model.url(); fn = fn || noop; if (!this.isValid()) return fn(new Error('validation failed')); - this.model.emit('saving', this); - this.emit('saving'); - request.post(url, self, function(res){ - if (res.error) return fn(error(res)); - if (res.body) self.primary(res.body.id); - self.dirty = {}; - self.model.emit('save', self); - self.emit('save'); - fn(); + this.run('saving', function() { + request.post(url, self, function(res){ + if (res.error) return fn(error(res)); + if (res.body) self.primary(res.body.id); + self.dirty = {}; + self.model.emit('save', self); + self.emit('save'); + fn(); + }); }); }; @@ -182,14 +182,14 @@ exports.update = function(fn){ var url = this.url(); fn = fn || noop; if (!this.isValid()) return fn(new Error('validation failed')); - this.model.emit('saving', this); - this.emit('saving'); - request.put(url, self, function(res){ - if (res.error) return fn(error(res)); - self.dirty = {}; - self.model.emit('save', self); - self.emit('save'); - fn(); + this.run('saving', function() { + request.put(url, self, function(res){ + if (res.error) return fn(error(res)); + self.dirty = {}; + self.model.emit('save', self); + self.emit('save'); + fn(); + }); }); }; @@ -275,4 +275,35 @@ exports.toJSON = function(){ function error(res) { return new Error('got ' + res.status + ' response'); -} \ No newline at end of file +} + +/** + * Run functions beforehand + * + * @param {String} event + * @param {Function} fn + * @api private + */ + +exports.run = function(event, done) { + var fns = this.model.listeners(event).concat(this.listeners(event)), + pending = 0; + + function next() { + if(!--pending) return done(); + } + + for (var i = 0, len = fns.length; i < len; i++) { + var fn = fns[i]; + if (fn.length > 1) { + pending++; + fn(this, next); + } else { + fn(this); + } + } + + if(!pending) return done(); +}; + + diff --git a/test/model.js b/test/model.js index 0b576a3..adb5493 100644 --- a/test/model.js +++ b/test/model.js @@ -167,7 +167,9 @@ describe('Model#remove()', function(){ var pet = new Pet({ name: 'Tobi' }); pet.save(function(err){ assert(!err); - pet.on('removing', done); + pet.on('removing', function() { + done(); + }); pet.remove(); }); }) @@ -231,6 +233,20 @@ describe('Model#save(fn)', function(){ }); pet.save(); }) + + it('should handle async plugins', function(done){ + var pet = new Pet({ name: 'Tobi', species: 'Ferret' }); + pet.on('saving', function(pet, next) { + setTimeout(function() { + next(); + }, 100); + }); + Pet.once('save', function(obj){ + assert(pet == obj); + done(); + }); + pet.save(); + }) }) describe('and invalid', function(){ @@ -270,7 +286,9 @@ describe('Model#save(fn)', function(){ var pet = new Pet({ name: 'Tobi', species: 'Ferret' }); pet.save(function(err){ assert(!err); - pet.on('saving', done); + pet.on('saving', function() { + done(); + }); pet.save(); }); }) @@ -382,4 +400,4 @@ describe('Model#isValid()', function(){ assert(true === user.isValid()); assert(0 == user.errors.length); }) -}) \ No newline at end of file +}) From 1be34ef08b6700f8b4dd17785c88d150b06232f7 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Wed, 19 Dec 2012 17:05:27 -0600 Subject: [PATCH 02/20] starting transport --- component.json | 5 +++-- lib/static.js | 43 +++++++++++++++++++++++++++---------------- lib/sync.js | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 lib/sync.js diff --git a/component.json b/component.json index c15f488..f3cab29 100644 --- a/component.json +++ b/component.json @@ -25,8 +25,9 @@ "scripts": [ "lib/index.js", "lib/static.js", - "lib/proto.js" + "lib/proto.js", + "lib/sync.js" ], "main": "lib/index.js", "license": "MIT" -} \ No newline at end of file +} diff --git a/lib/static.js b/lib/static.js index 29f64d2..32e8b2d 100644 --- a/lib/static.js +++ b/lib/static.js @@ -3,7 +3,8 @@ * Module dependencies. */ -var request = require('superagent') +var sync = require('./sync') + , request = require('superagent') , Collection = require('collection') , noop = function(){}; @@ -110,17 +111,22 @@ exports.removeAll = function(fn){ * @api public */ -exports.all = function(fn){ - var self = this; - var url = this.url('all'); - request.get(url, function(res){ - if (res.error) return fn(error(res)); +exports.all = function() { + var args = [].slice.call(arguments), + fn = args.pop(), + self = this, + read = this.read || sync.read; + + read.apply(this, args.concat(res)); + + function res(err, items) { + if (err) return fn(err); var col = new Collection; - for (var i = 0, len = res.body.length; i < len; ++i) { - col.push(new self(res.body[i])); + for (var i = 0, len = items.length; i < len; ++i) { + col.push(new self(items[i])); } fn(null, col); - }); + } }; /** @@ -131,14 +137,19 @@ exports.all = function(fn){ * @api public */ -exports.get = function(id, fn){ - var self = this; - var url = this.url(id); - request.get(url, function(res){ - if (res.error) return fn(error(res)); - var model = new self(res.body); +exports.get = function() { + var args = [].slice.call(arguments), + fn = args.pop(), + self = this, + read = this.read || sync.read; + + read.apply(this, args.concat(res)); + + function res(err, item) { + if(err) return fn(err); + var model = new self(item); fn(null, model); - }); + } }; /** diff --git a/lib/sync.js b/lib/sync.js new file mode 100644 index 0000000..cd51313 --- /dev/null +++ b/lib/sync.js @@ -0,0 +1,41 @@ +/** + * Module dependencies + */ + +var request = require('superagent'); + +/** + * Create + */ + +exports.create = function() { + +}; + +/** + * Read + */ + +exports.read = function(id, fn) { + if(arguments.length == 1) fn = id; + var url = this.url('all'); + request.get(url, function(res) { + return fn(res.error, res.body); + }); +}; + +/** + * Update an item + */ + +exports.update = function() { + +}; + +/** + * Destroy + */ + +exports.destroy = function() { + +}; From 66eccbc7e732c4550b4bd211b7155b599f97b0dd Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Wed, 19 Dec 2012 18:30:23 -0600 Subject: [PATCH 03/20] abstracted transport out from component/model --- lib/proto.js | 69 ++++++++++++++++++++++++++++++++------------------- lib/static.js | 28 ++++++++++++--------- lib/sync.js | 39 ++++++++++++++++++++++------- test/model.js | 2 +- 4 files changed, 90 insertions(+), 48 deletions(-) diff --git a/lib/proto.js b/lib/proto.js index e351684..0698a9c 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -3,7 +3,8 @@ * Module dependencies. */ -var Emitter = require('emitter') +var sync = require('./sync') + , Emitter = require('emitter') , request = require('superagent') , JSON = require('json') , each = require('each') @@ -124,20 +125,25 @@ exports.validate = function(){ * @api public */ -exports.remove = function(fn){ - fn = fn || noop; +exports.remove = function(){ + var args = [].slice.call(arguments), + fn = args.pop() || noop, + self = this, + destroy = this.model.destroy || sync.destroy; + if (this.isNew()) return fn(new Error('not saved')); - var self = this; - var url = this.url(); + this.model.emit('removing', this); this.emit('removing'); - request.del(url, function(res){ - if (res.error) return fn(error(res)); + destroy.apply(this, args.concat(res)); + + function res(err, body) { + if(err) return fn(error(err)); self.removed = true; self.model.emit('remove', self); self.emit('remove'); fn(); - }); + } }; /** @@ -152,22 +158,28 @@ exports.remove = function(fn){ * @api public */ -exports.save = function(fn){ - if (!this.isNew()) return this.update(fn); - var self = this; - var url = this.model.url(); - fn = fn || noop; +exports.save = function(){ + if (!this.isNew()) return this.update.apply(this, arguments); + + var args = [].slice.call(arguments), + fn = args.pop() || noop, + self = this, + create = this.model.create || sync.create; + if (!this.isValid()) return fn(new Error('validation failed')); + this.model.emit('saving', this); this.emit('saving'); - request.post(url, self, function(res){ - if (res.error) return fn(error(res)); - if (res.body) self.primary(res.body.id); + create.apply(this, args.concat(res)); + + function res(err, body) { + if (err) return fn(error(err)); + if (body) self.primary(body.id); self.dirty = {}; self.model.emit('save', self); self.emit('save'); fn(); - }); + } }; /** @@ -177,20 +189,25 @@ exports.save = function(fn){ * @api private */ -exports.update = function(fn){ - var self = this; - var url = this.url(); - fn = fn || noop; +exports.update = function() { + var args = [].slice.call(arguments), + fn = args.pop() || noop, + self = this, + update = this.model.update || sync.update; + if (!this.isValid()) return fn(new Error('validation failed')); + this.model.emit('saving', this); this.emit('saving'); - request.put(url, self, function(res){ - if (res.error) return fn(error(res)); + update.apply(this, args.concat(res)); + + function res(err, body) { + if(err) return fn(error(err)); self.dirty = {}; self.model.emit('save', self); self.emit('save'); - fn(); - }); + return fn(); + } }; /** @@ -275,4 +292,4 @@ exports.toJSON = function(){ function error(res) { return new Error('got ' + res.status + ' response'); -} \ No newline at end of file +} diff --git a/lib/static.js b/lib/static.js index 32e8b2d..c498c4c 100644 --- a/lib/static.js +++ b/lib/static.js @@ -94,14 +94,18 @@ exports.attr = function(name, options){ * @api public */ -exports.removeAll = function(fn){ - fn = fn || noop; - var self = this; - var url = this.url('all'); - request.del(url, function(res){ - if (res.error) return fn(error(res)); +exports.removeAll = function() { + var args = [].slice.call(arguments), + fn = args.pop() || noop, + self = this, + destroy = this.destroy || sync.destroy; + + destroy.apply(this, args.concat(res)); + + function res(err, body) { + if(err) return fn(error(err)); fn(); - }); + } }; /** @@ -119,11 +123,11 @@ exports.all = function() { read.apply(this, args.concat(res)); - function res(err, items) { + function res(err, body) { if (err) return fn(err); var col = new Collection; - for (var i = 0, len = items.length; i < len; ++i) { - col.push(new self(items[i])); + for (var i = 0, len = body.length; i < len; ++i) { + col.push(new self(body[i])); } fn(null, col); } @@ -145,9 +149,9 @@ exports.get = function() { read.apply(this, args.concat(res)); - function res(err, item) { + function res(err, body) { if(err) return fn(err); - var model = new self(item); + var model = new self(body); fn(null, model); } }; diff --git a/lib/sync.js b/lib/sync.js index cd51313..4942f5e 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -1,3 +1,7 @@ +/** + * AJAX Transport + */ + /** * Module dependencies */ @@ -8,8 +12,11 @@ var request = require('superagent'); * Create */ -exports.create = function() { - +exports.create = function(fn) { + var url = this.model.url(); + request.post(url, this, function(res) { + fn(res.error, res.body); + }); }; /** @@ -17,25 +24,39 @@ exports.create = function() { */ exports.read = function(id, fn) { - if(arguments.length == 1) fn = id; - var url = this.url('all'); + var url; + + if (arguments.length == 1) { + fn = id; + url = this.url('all'); + } else { + url = this.url(id); + } + request.get(url, function(res) { - return fn(res.error, res.body); + fn(res.error, res.body); }); }; /** - * Update an item + * Update */ -exports.update = function() { - +exports.update = function(fn) { + var url = this.url(); + request.put(url, this, function(res) { + fn(res.error, res.body); + }); }; /** * Destroy */ -exports.destroy = function() { +exports.destroy = function(fn) { + var url = (this.model) ? this.url() : this.url('all'); + request.del(url, function(res) { + fn(res.error, res.body); + }); }; diff --git a/test/model.js b/test/model.js index 0b576a3..2fd3fb2 100644 --- a/test/model.js +++ b/test/model.js @@ -382,4 +382,4 @@ describe('Model#isValid()', function(){ assert(true === user.isValid()); assert(0 == user.errors.length); }) -}) \ No newline at end of file +}) From 43f52ffefd6668a115f47a4b7b7098bcab53b24a Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 01:35:59 -0600 Subject: [PATCH 04/20] sync api now the same as model --- lib/index.js | 1 + lib/proto.js | 10 +++++----- lib/static.js | 25 +++++++++++++++++++------ lib/sync.js | 51 ++++++++++++++++++++++++++++++++------------------- 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/lib/index.js b/lib/index.js index ca3eb13..b4c2838 100644 --- a/lib/index.js +++ b/lib/index.js @@ -48,6 +48,7 @@ function createModel(name) { model.modelName = name; model.base = '/' + name.toLowerCase(); model.attrs = {}; + model.sync = {}; model.validators = []; for (var key in statics) model[key] = statics[key]; diff --git a/lib/proto.js b/lib/proto.js index eb2935c..8c1641e 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -129,12 +129,12 @@ exports.remove = function(){ var args = [].slice.call(arguments), fn = args.pop() || noop, self = this, - destroy = this.model.destroy || sync.destroy; + remove = this.model.sync.remove || sync.remove; if (this.isNew()) return fn(new Error('not saved')); this.run('removing', function() { - destroy.apply(self, args.concat(res)); + remove.apply(self, args.concat(res)); }); function res(err, body) { @@ -164,12 +164,12 @@ exports.save = function(){ var args = [].slice.call(arguments), fn = args.pop() || noop, self = this, - create = this.model.create || sync.create; + save = this.model.sync.save || sync.save; if (!this.isValid()) return fn(new Error('validation failed')); this.run('saving', function() { - create.apply(self, args.concat(res)); + save.apply(self, args.concat(res)); }); function res(err, body) { @@ -193,7 +193,7 @@ exports.update = function() { var args = [].slice.call(arguments), fn = args.pop() || noop, self = this, - update = this.model.update || sync.update; + update = this.model.sync.update || sync.update; if (!this.isValid()) return fn(new Error('validation failed')); diff --git a/lib/static.js b/lib/static.js index c498c4c..58be67c 100644 --- a/lib/static.js +++ b/lib/static.js @@ -98,9 +98,9 @@ exports.removeAll = function() { var args = [].slice.call(arguments), fn = args.pop() || noop, self = this, - destroy = this.destroy || sync.destroy; + removeAll = this.sync.removeAll || sync.removeAll; - destroy.apply(this, args.concat(res)); + removeAll.apply(this, args.concat(res)); function res(err, body) { if(err) return fn(error(err)); @@ -119,9 +119,9 @@ exports.all = function() { var args = [].slice.call(arguments), fn = args.pop(), self = this, - read = this.read || sync.read; + all = this.sync.all || sync.all; - read.apply(this, args.concat(res)); + all.apply(this, args.concat(res)); function res(err, body) { if (err) return fn(err); @@ -145,9 +145,9 @@ exports.get = function() { var args = [].slice.call(arguments), fn = args.pop(), self = this, - read = this.read || sync.read; + get = this.sync.get || sync.get; - read.apply(this, args.concat(res)); + get.apply(this, args.concat(res)); function res(err, body) { if(err) return fn(err); @@ -156,6 +156,19 @@ exports.get = function() { } }; +/** + * Set up a sync + * + * @param {Object} obj + * @return {Function} self + * @api public + */ + +exports.sync = function(obj) { + this.sync = obj; + return this; +}; + /** * Response error helper. * diff --git a/lib/sync.js b/lib/sync.js index 4942f5e..ba901d7 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -9,37 +9,51 @@ var request = require('superagent'); /** - * Create + * All */ -exports.create = function(fn) { - var url = this.model.url(); - request.post(url, this, function(res) { +exports.all = function(fn) { + var url = this.url('all'); + request.get(url, function(res) { fn(res.error, res.body); }); }; /** - * Read + * Get */ -exports.read = function(id, fn) { - var url; +exports.get = function(id, fn) { + var url = this.url(id); + request.get(url, function(res) { + fn(res.error, res.body); + }); +}; - if (arguments.length == 1) { - fn = id; - url = this.url('all'); - } else { - url = this.url(id); - } +/** + * removeAll + */ - request.get(url, function(res) { +exports.removeAll = function(fn) { + var url = this.url('all'); + request.del(url, function(res) { fn(res.error, res.body); }); }; /** - * Update + * save + */ + +exports.save = function(fn) { + var url = this.model.url(); + request.post(url, this, function(res) { + fn(res.error, res.body); + }); +}; + +/** + * update */ exports.update = function(fn) { @@ -50,12 +64,11 @@ exports.update = function(fn) { }; /** - * Destroy + * remove */ -exports.destroy = function(fn) { - var url = (this.model) ? this.url() : this.url('all'); - +exports.remove = function(fn) { + var url = this.url(); request.del(url, function(res) { fn(res.error, res.body); }); From e7102cd057c756e46186b104fdc763ac49ed362d Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 01:40:21 -0600 Subject: [PATCH 05/20] published model-component2 --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5cf46aa..cbe57d4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "model", - "version": "0.0.0", + "name": "model-component2", + "version": "0.0.1", "devDependencies": { "express": "3.0.0" } -} \ No newline at end of file +} From 429682f824e13366d6900cb64bb3ae1e3dabe0a2 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 01:44:23 -0600 Subject: [PATCH 06/20] removed sync --- lib/static.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/static.js b/lib/static.js index 58be67c..816eaee 100644 --- a/lib/static.js +++ b/lib/static.js @@ -156,19 +156,6 @@ exports.get = function() { } }; -/** - * Set up a sync - * - * @param {Object} obj - * @return {Function} self - * @api public - */ - -exports.sync = function(obj) { - this.sync = obj; - return this; -}; - /** * Response error helper. * From dda1ad6643b2008e6559c71e442d3864ce3c6e58 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 02:00:02 -0600 Subject: [PATCH 07/20] published under node-model --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cbe57d4..3366d8e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "model-component2", + "name": "node-model", "version": "0.0.1", "devDependencies": { "express": "3.0.0" From d23ca6d2ec8b84d7b2d17db402cc612099fd8c31 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 15:04:07 -0600 Subject: [PATCH 08/20] removing unused deps, new package name --- component.json | 2 +- lib/proto.js | 2 -- lib/static.js | 1 - package.json | 8 +++++++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/component.json b/component.json index f3cab29..64834c2 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "model", "repo": "component/model", "description": "Elegant data models", - "version": "0.0.1", + "version": "0.0.3", "keywords": [ "collection", "model", diff --git a/lib/proto.js b/lib/proto.js index 8c1641e..de2b1c2 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -5,8 +5,6 @@ var sync = require('./sync') , Emitter = require('emitter') - , request = require('superagent') - , JSON = require('json') , each = require('each') , noop = function(){}; diff --git a/lib/static.js b/lib/static.js index 816eaee..8e0cb80 100644 --- a/lib/static.js +++ b/lib/static.js @@ -4,7 +4,6 @@ */ var sync = require('./sync') - , request = require('superagent') , Collection = require('collection') , noop = function(){}; diff --git a/package.json b/package.json index 3366d8e..889b25f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,13 @@ { - "name": "node-model", + "name": "model-matthewmueller", + "repo" : "matthewmueller/model", "version": "0.0.1", + "main": "lib/index.js", "devDependencies": { "express": "3.0.0" + }, + "dependencies": { + "superagent": "~0.10.0", + "emitter-component": "0.0.6" } } From d5dc84e47ba57bcbf720b8b57f89900958baf770 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 15:04:28 -0600 Subject: [PATCH 09/20] fixed package --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 889b25f..bd45254 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "model-matthewmueller", + "name": "model", "repo" : "matthewmueller/model", "version": "0.0.1", "main": "lib/index.js", From 1a21d129971978ea2264685e280b604a374a856f Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 15:25:54 -0600 Subject: [PATCH 10/20] package.json --- package.json | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index bd45254..da1eadb 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "model", - "repo" : "matthewmueller/model", "version": "0.0.1", "main": "lib/index.js", "devDependencies": { "express": "3.0.0" }, "dependencies": { - "superagent": "~0.10.0", - "emitter-component": "0.0.6" + "each": "component/each", + "superagent": "visionmedia/superagent", + "emitter" : "component/emitter", + "collection" : "component/collection", + "json" : "component/json" } } From 367dacf9836b6b5013501bfae5864ed11162861b Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Fri, 21 Dec 2012 15:28:37 -0600 Subject: [PATCH 11/20] rmoeved collection --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index da1eadb..25596ea 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "each": "component/each", "superagent": "visionmedia/superagent", "emitter" : "component/emitter", - "collection" : "component/collection", "json" : "component/json" } } From e4371d576936a0ed94a791322a730bb8377990e7 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 22:05:44 -0600 Subject: [PATCH 12/20] component and node implementation --- component.json | 4 ++-- lib/common.js | 9 +++++++++ lib/index.js | 7 ++++--- lib/proto.js | 9 +++++---- lib/static.js | 7 ++++--- package.json | 8 ++++---- 6 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 lib/common.js diff --git a/component.json b/component.json index 64834c2..d97d4b2 100644 --- a/component.json +++ b/component.json @@ -12,10 +12,10 @@ ], "dependencies": { "component/each": "*", - "component/json": "*", "component/emitter": "*", "component/collection": "*", - "visionmedia/superagent": "*" + "visionmedia/superagent": "*", + "ForbesLindesay/is-browser": "*" }, "development": { "component/reactive": "*", diff --git a/lib/common.js b/lib/common.js new file mode 100644 index 0000000..803ca8a --- /dev/null +++ b/lib/common.js @@ -0,0 +1,9 @@ +if(require('is-browser')) { + exports.emitter = require('emitter'); + exports.collection = require('collection'); + exports.each = require('each'); +} else { + exports.emitter = require('emitter-component'); + exports.collection = Array; + exports.each = require('each-component'); +} diff --git a/lib/index.js b/lib/index.js index b4c2838..310ec62 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,9 +3,10 @@ * Module dependencies. */ -var proto = require('./proto') - , statics = require('./static') - , Emitter = require('emitter'); +var proto = require('./proto'), + statics = require('./static'), + common = require('./common'), + Emitter = common.emitter; /** * Expose `createModel`. diff --git a/lib/proto.js b/lib/proto.js index de2b1c2..c28445c 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -3,10 +3,11 @@ * Module dependencies. */ -var sync = require('./sync') - , Emitter = require('emitter') - , each = require('each') - , noop = function(){}; +var sync = require('./sync'), + common = require('./common'), + Emitter = common.emitter, + each = common.each, + noop = function(){}; /** * Mixin emitter. diff --git a/lib/static.js b/lib/static.js index 8e0cb80..7baca41 100644 --- a/lib/static.js +++ b/lib/static.js @@ -3,9 +3,10 @@ * Module dependencies. */ -var sync = require('./sync') - , Collection = require('collection') - , noop = function(){}; +var sync = require('./sync'), + common = require('./common'), + Collection = common.collection, + noop = function(){}; /** * Construct a url to the given `path`. diff --git a/package.json b/package.json index 25596ea..f992355 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,9 @@ "express": "3.0.0" }, "dependencies": { - "each": "component/each", - "superagent": "visionmedia/superagent", - "emitter" : "component/emitter", - "json" : "component/json" + "is-browser": "~1.0.0", + "emitter-component": "0.0.6", + "each-component": "~0.0.1", + "superagent": "~0.10.0" } } From 1ba92a1a2829f8ebfb27ac3ff848453d3ada888f Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 22:09:30 -0600 Subject: [PATCH 13/20] common working on brwoser --- component.json | 1 + 1 file changed, 1 insertion(+) diff --git a/component.json b/component.json index d97d4b2..d0837e4 100644 --- a/component.json +++ b/component.json @@ -26,6 +26,7 @@ "lib/index.js", "lib/static.js", "lib/proto.js", + "lib/common.js", "lib/sync.js" ], "main": "lib/index.js", From 69c639c6f997b4ae0b4aed9507e8930b80f55310 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 22:41:48 -0600 Subject: [PATCH 14/20] node.js working now --- lib/common.js | 4 +++- package.json | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/common.js b/lib/common.js index 803ca8a..5e31bc9 100644 --- a/lib/common.js +++ b/lib/common.js @@ -5,5 +5,7 @@ if(require('is-browser')) { } else { exports.emitter = require('emitter-component'); exports.collection = Array; - exports.each = require('each-component'); + exports.each = function(arr, fn) { + return arr.forEach(fn); + }; } diff --git a/package.json b/package.json index f992355..05bb037 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "dependencies": { "is-browser": "~1.0.0", "emitter-component": "0.0.6", - "each-component": "~0.0.1", "superagent": "~0.10.0" } } From 5a223145c5b6e1e70a5d8611806e3503a5b9724b Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 22:55:43 -0600 Subject: [PATCH 15/20] version --- component.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/component.json b/component.json index d0837e4..899741f 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "model", "repo": "component/model", "description": "Elegant data models", - "version": "0.0.3", + "version": "0.0.1", "keywords": [ "collection", "model", From 63a9a31fb275883f2033eb0b4ba758330624feea Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 23:07:37 -0600 Subject: [PATCH 16/20] published node-model --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 05bb037..93a8b3d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "model", + "name": "node-model", "version": "0.0.1", "main": "lib/index.js", "devDependencies": { From 7e8f126daf2358f2317ff615402fc98732707df6 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 23:54:40 -0600 Subject: [PATCH 17/20] only update if changed --- lib/proto.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/proto.js b/lib/proto.js index c28445c..1905d40 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -192,8 +192,10 @@ exports.update = function() { var args = [].slice.call(arguments), fn = args.pop() || noop, self = this, + changed = this.changed(), update = this.model.sync.update || sync.update; + if(!changed) return fn(null, this); if (!this.isValid()) return fn(new Error('validation failed')); this.run('saving', function() { From abf4ca8a250fc46eccfd08502c367701b2e32f32 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Sun, 23 Dec 2012 23:56:32 -0600 Subject: [PATCH 18/20] removed extraneous arg --- lib/proto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proto.js b/lib/proto.js index 1905d40..0933b86 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -202,7 +202,7 @@ exports.update = function() { update.apply(self, args.concat(res)); }); - function res(err, body) { + function res(err) { if(err) return fn(error(err)); self.dirty = {}; self.model.emit('save', self); From 4ce4747dfc7884ca3bc2476623d4c61dc9c859ac Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Tue, 25 Dec 2012 22:57:23 -0600 Subject: [PATCH 19/20] added body._id --- lib/proto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proto.js b/lib/proto.js index 0933b86..ff9dc8c 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -173,7 +173,7 @@ exports.save = function(){ function res(err, body) { if (err) return fn(error(err)); - if (body) self.primary(body.id); + if (body) self.primary(body.id || body._id); self.dirty = {}; self.model.emit('save', self); self.emit('save'); From e94b200b86d9e78d733f6b580949dc6624d2d6d8 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Tue, 25 Dec 2012 22:58:03 -0600 Subject: [PATCH 20/20] Release 0.0.2 --- component.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/component.json b/component.json index 899741f..cfe6844 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "model", "repo": "component/model", "description": "Elegant data models", - "version": "0.0.1", + "version": "0.0.2", "keywords": [ "collection", "model", diff --git a/package.json b/package.json index 93a8b3d..c47078b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-model", - "version": "0.0.1", + "version": "0.0.2", "main": "lib/index.js", "devDependencies": { "express": "3.0.0"