Skip to content
Browse files

`update` should trigger "update" event, add separate `merge` method.

  • Loading branch information...
1 parent 6966778 commit 63f4a3a48557d93aa337adbf26d5446c39a427d3 @benpickles committed Feb 12, 2010
Showing with 44 additions and 8 deletions.
  1. +1 −0 CHANGELOG.markdown
  2. +7 −2 src/model.js
  3. +6 −6 src/model_rest_persistence.js
  4. +30 −0 test/model_rest_persistence.js
View
1 CHANGELOG.markdown
@@ -1,5 +1,6 @@
# Changelog
+* `update` should trigger "update" event, add separate `merge` method.
* Fix that persistence failure should not trigger corresponding event.
## 0.6.0
View
9 src/model.js
@@ -100,6 +100,11 @@ var Model = function(name, methods) {
return this.attributes.id || null;
},
+ merge: function(attributes) {
+ $.extend(this.attributes, attributes);
+ return this;
+ },
+
newRecord: function() {
return this.id() == null;
},
@@ -113,7 +118,7 @@ var Model = function(name, methods) {
if (!this.valid()) return false;
// Merge any changes into attributes and clear changes.
- this.update(this.changes).reset();
+ this.merge(this.changes).reset();
var method = this.newRecord() ? "create" : "update";
this.callPersistMethod(method, callback);
@@ -138,7 +143,7 @@ var Model = function(name, methods) {
},
update: function(attributes) {
- $.extend(this.attributes, attributes);
+ this.merge(attributes).trigger("update");
return this;
},
View
12 src/model_rest_persistence.js
@@ -6,9 +6,9 @@ Model.RestPersistence = function(resource, methods) {
model_resource.prototype = $.extend({
create: function(model, callback) {
var wrappedCallback = function(success, data, xhr) {
- // Remote data is the definitive source, merge model attributes with
- // response data.
- this.update(data);
+ // Remote data is the definitive source, merge response data with
+ // model attributes.
+ this.merge(data);
// Execute callback if suppied.
if (callback) callback.apply(this, arguments);
@@ -31,9 +31,9 @@ Model.RestPersistence = function(resource, methods) {
update: function(model, callback) {
var wrappedCallback = function(success, data, xhr) {
- // Remote data is the definitive source, merge model attributes with
- // response data.
- this.update(data);
+ // Remote data is the definitive source, merge response data with
+ // model attributes.
+ this.merge(data);
// Execute callback if suppied.
if (callback) callback.apply(this, arguments);
View
30 test/model_rest_persistence.js
@@ -83,3 +83,33 @@ test("Model#destroy()", function() {
equals(request.dataType, "json");
same(request.data, null);
});
+
+test("events", function() {
+ var Post = Model("post", {
+ persistence: Model.RestPersistence("./post.json", {
+ update_path: function() { return this.create_path(); }
+ })
+ });
+
+ var events = [];
+
+ // Stub trigger and capture its argument.
+ Post.prototype.trigger = function(name) {
+ events.push(name);
+ };
+
+ var post = new Post();
+
+ AjaxSpy.stubData({ id: 1 });
+
+ stop();
+
+ post.save(function() {
+ same(events.join(", "), "initialize, create");
+
+ post.save(function() {
+ same(events.join(", "), "initialize, create, update");
+ start();
+ });
+ });
+});

0 comments on commit 63f4a3a

Please sign in to comment.
Something went wrong with that request. Please try again.