Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gem 'highlight', :require => 'simplabs/highlight'
gem 'nokogiri'
gem 'rdiscount'
gem 'sinatra'
gem 'json'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GEM
rack
highlight (1.1.2)
activesupport (>= 2.0.0)
json (1.5.1)
nokogiri (1.4.4)
rack (1.2.1)
rdiscount (1.6.8)
Expand All @@ -22,6 +23,7 @@ DEPENDENCIES
closure-compiler
fewer (~> 0.2.0)
highlight
json
nokogiri
rdiscount
sinatra
57 changes: 57 additions & 0 deletions src/model_class_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,52 @@ Model.ClassMethods = {
return this
}
},

callPersistMethod: function(method, model, callback) {
var self = this;

// Automatically manage adding and removing from the model's Collection.
var manageCollection = function() {
if (method === "destroy") {
self.remove(model)
} else {
self.add(model)
}
};

// Wrap the existing callback in this function so we always manage the
// collection and trigger events from here rather than relying on the
// persistence adapter to do it for us. The persistence adapter is
// only required to execute the callback with a single argument - a
// boolean to indicate whether the call was a success - though any
// other arguments will also be forwarded to the original callback.
var wrappedCallback = function(success) {
if (success) {
// Merge any changes into attributes and clear changes.
model.merge(model.changes).reset();

// Add/remove from collection if persist was successful.
manageCollection();

// Trigger the event before executing the callback.
model.trigger(method);
}

// Store the return value of the callback.
var value;

// Run the supplied callback.
if (callback) value = callback.apply(model, arguments);

return value;
};

if (this._persistence) {
this._persistence[method](model, wrappedCallback);
} else {
wrappedCallback.call(model, true);
}
},

pluck: function(attribute) {
var all = this.all()
Expand Down Expand Up @@ -125,6 +171,17 @@ Model.ClassMethods = {
return false;
}
},

// Persistence CRUD
// Instances CRUD methods delegate here
destroy: function(model, callback) {
this.callPersistMethod("destroy", model, callback);
},

save: function(model, callback) {
var method = model.newRecord() ? "create" : "update";
this.callPersistMethod(method, model, callback);
},

reverse: function() {
return this.chain(this.all().reverse())
Expand Down
51 changes: 2 additions & 49 deletions src/model_instance_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,8 @@ Model.InstanceMethods = {
}
},

callPersistMethod: function(method, callback) {
var self = this;

// Automatically manage adding and removing from the model's Collection.
var manageCollection = function() {
if (method === "destroy") {
self.constructor.remove(self)
} else {
self.constructor.add(self)
}
};

// Wrap the existing callback in this function so we always manage the
// collection and trigger events from here rather than relying on the
// persistence adapter to do it for us. The persistence adapter is
// only required to execute the callback with a single argument - a
// boolean to indicate whether the call was a success - though any
// other arguments will also be forwarded to the original callback.
var wrappedCallback = function(success) {
if (success) {
// Merge any changes into attributes and clear changes.
self.merge(self.changes).reset();

// Add/remove from collection if persist was successful.
manageCollection();

// Trigger the event before executing the callback.
self.trigger(method);
}

// Store the return value of the callback.
var value;

// Run the supplied callback.
if (callback) value = callback.apply(self, arguments);

return value;
};

if (this.constructor._persistence) {
this.constructor._persistence[method](this, wrappedCallback);
} else {
wrappedCallback.call(this, true);
}
},

destroy: function(callback) {
this.callPersistMethod("destroy", callback);
this.constructor.destroy(this, callback);
return this;
},

Expand All @@ -102,8 +56,7 @@ Model.InstanceMethods = {

save: function(callback) {
if (this.valid()) {
var method = this.newRecord() ? "create" : "update";
this.callPersistMethod(method, callback);
this.constructor.save(this, callback);
} else if (callback) {
callback(false);
}
Expand Down