Skip to content

Commit

Permalink
update should trigger "update" event, add separate merge method.
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Feb 12, 2010
1 parent 6966778 commit 63f4a3a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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);
Expand All @@ -138,7 +143,7 @@ var Model = function(name, methods) {
},

update: function(attributes) {
$.extend(this.attributes, attributes);
this.merge(attributes).trigger("update");
return this;
},

Expand Down
12 changes: 6 additions & 6 deletions src/model_rest_persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions test/model_rest_persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.