Skip to content

Commit

Permalink
refactored using &&
Browse files Browse the repository at this point in the history
  • Loading branch information
weepy committed Mar 10, 2011
1 parent c78a0e6 commit 5cab3a7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
22 changes: 11 additions & 11 deletions lib/core.js
Expand Up @@ -69,14 +69,17 @@ module.exports = function(type, properties, store) {
*/
Model.create = function(props, cb) {
var m = new Model(props)
m.save(cb || noop)
m.save(cb)
}

/** Find an object or create it if it doesn't exist
*/
Model.find_or_create = function(props, cb) {
Model.find(props.id, function(m) {
m ? cb(m) : Model.create(props, cb || noop)
if(m)
cb && cb(m)
else
Model.create(props, cb)
})
}

Expand Down Expand Up @@ -120,15 +123,14 @@ module.exports = function(type, properties, store) {
*/
Model.prototype.update = function(params, cb) {
params || (params == {})
cb || (cb == noop)

Model.find(params.id, function(m) {
if(!m) {
cb()
cb && cb()
} else {
m.merge(params)
m.save(function(ok) {
cb.call(m, ok)
cb && cb.call(m, ok)
})
}
})
Expand All @@ -140,10 +142,9 @@ module.exports = function(type, properties, store) {
*/
Model.prototype.save = function(cb) {
var self = this
cb || (cb = noop)
utils.achain.call(self, self._saveStack, [], function(err, results) {
if(!err) self._synchronize()
cb.call(self, self)
cb && cb.call(self, self)
})
}

Expand All @@ -161,10 +162,9 @@ module.exports = function(type, properties, store) {
*/
Model.prototype.destroy = function(cb) {
var self = this
cb || (cb = noop)
Model.destroy(this.id, function(ok) {
if(ok) self.trigger("destroyed")
cb(ok)
cb && cb(ok)
})
}

Expand Down Expand Up @@ -210,7 +210,7 @@ module.exports = function(type, properties, store) {
this.errors = []
this._property_validate()
this.trigger("saving").complete(function() {
cb.call(this, !this.in_error())
cb && cb.call(this, !this.in_error())
})
}

Expand All @@ -232,7 +232,7 @@ module.exports = function(type, properties, store) {
Model.prototype._finalize_save = function(cb) {
if(this.in_error()) {
this.trigger("error", "saving failed").complete(function(){
cb(false)
cb && cb(false)
})
} else {
this._synchronize()
Expand Down
4 changes: 2 additions & 2 deletions lib/stores/memory.js
Expand Up @@ -32,9 +32,9 @@ exports.mixin = function(Model) {
Model.destroy = function(id, cb) {
if(Model.DB[id]) {
delete Model.DB[id]
cb(true)
cb && cb(true)
}
else cb(false)
else cb && cb(false)
}

Model.count = function(cb) {
Expand Down
3 changes: 1 addition & 2 deletions lib/stores/redis.js
Expand Up @@ -15,7 +15,7 @@ exports.mixin = function(Model) {
Model.destroy = function(id, cb) {
Model.client.del(Model.type + ":" + id, function(err, data) {
Model.client.ZREM(Model.type +"s", id, function(err, data) {
cb()
cb && cb()
})
})
}
Expand Down Expand Up @@ -178,7 +178,6 @@ exports.mixin = function(Model) {
}

Model.client.ZADD(this.key() +":"+type, fscore, id, function(err, data) {
console.log(err, data, "X")
cb && cb(!err)
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/stores/rest.js
Expand Up @@ -36,7 +36,7 @@ exports.mixin = function(Model) {
Model.destroy = function(id, cb) {
var self = this
Model.ajax.post(Model.url + "/" + id + "/destroy", {}, function(o) {
cb(o)
cb && cb(o)
})
}
}

0 comments on commit 5cab3a7

Please sign in to comment.