From 38191f4982f140704e38ca3b7ea3a9b22fc61b4c Mon Sep 17 00:00:00 2001 From: Ismael Celis Date: Wed, 11 May 2011 15:09:58 +0100 Subject: [PATCH 1/2] JSON is needed for testing but it wasn't declared in Gemfile --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index bb20fcf..b122635 100644 --- a/Gemfile +++ b/Gemfile @@ -6,3 +6,4 @@ gem 'highlight', :require => 'simplabs/highlight' gem 'nokogiri' gem 'rdiscount' gem 'sinatra' +gem 'json' diff --git a/Gemfile.lock b/Gemfile.lock index ec717ee..f7372d8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -22,6 +23,7 @@ DEPENDENCIES closure-compiler fewer (~> 0.2.0) highlight + json nokogiri rdiscount sinatra From c99d14b81b85cf12462dc029ee8d094dfc91128e Mon Sep 17 00:00:00 2001 From: Ismael Celis Date: Wed, 11 May 2011 15:48:45 +0100 Subject: [PATCH 2/2] Moved instance persistance to new class methods .destroy() and .save(). Each take an instance and callback. --- src/model_class_methods.js | 57 +++++++++++++++++++++++++++++++++++ src/model_instance_methods.js | 51 ++----------------------------- 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/src/model_class_methods.js b/src/model_class_methods.js index f346a17..f318527 100644 --- a/src/model_class_methods.js +++ b/src/model_class_methods.js @@ -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() @@ -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()) diff --git a/src/model_instance_methods.js b/src/model_instance_methods.js index a1f30e7..db79d56 100644 --- a/src/model_instance_methods.js +++ b/src/model_instance_methods.js @@ -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; }, @@ -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); }