Skip to content

Commit

Permalink
Added #update with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ratnikov committed Feb 25, 2009
1 parent 6b1a9c2 commit fd554b8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/base.js
Expand Up @@ -66,6 +66,12 @@ AjaxResource.Base.prototype.parse_json = function(json) {
};

AjaxResource.Base.prototype.create = function(success_callback) {

// sanity check
if (!this.is_new()) {
throw("Cannot create an existing record");
}

var post_data = jQuery.extend({ _method : 'post' }, this.serialized_attributes());
var resource = this;

Expand All @@ -83,3 +89,28 @@ AjaxResource.Base.prototype.create = function(success_callback) {
dataType: 'json'
});
};

AjaxResource.Base.prototype.update = function(success_callback) {

// sanity check
if (this.is_new()) {
throw("Cannot update a new record");
}

var post_data = jQuery.extend({ _method : 'put' }, this.serialized_attributes());
var resource = this;

return jQuery.ajax({
type: 'POST',
url : this.member_path(this.id()),
data: post_data,
success: function(json) {
if (resource.parse_json(json)) {
success_callback(resource);
} else {
// do nothing on error
}
},
dataType: 'json'
});
};
38 changes: 38 additions & 0 deletions test/base_test.js
Expand Up @@ -111,4 +111,42 @@ jQuery(document).ready(function() {
ajax_success({});
ok(!callback_invoked, "Should not invoke callback if it's missing the resource name");
});

module("#update");

test("Should make a PUT member_path ajax request and parse its returns", function() {
model.attributes().foo = 'foo';
model.attributes().id = 5;

var callback_invoked = false;
var callback = function(updated_model) {
equals(updated_model, model, "Should reference same model");
callback_invoked = true;
};

equals(model.update(callback), 'ajax_request', "Should return the ajax request object");

ok(typeof ajax_options !== "undefined", "Should have made an ajax request");

equals(ajax_options.type, "POST", "Should use POST request");
equals(ajax_options.url, model.member_path(model.id()), "Should use member path with model id as url");
equals(ajax_options.dataType, "json", "Should expect json as response");

equals(ajax_options.data._method, 'put', "Should specify to use 'put' method for rails resource to recognize");
delete ajax_options.data._method;
same(ajax_options.data, model.serialized_attributes(), "Should use serialized attributes as rest of posted data");

var ajax_success = ajax_options.success;
ok(typeof ajax_success !== "undefined", "Should specify an ajax success callback");

ok(!callback_invoked, "Should not have invoked callback just yet");

var response = {};
response[model.resource_name()] = { id : 5, foo : 'fooism', html : 'html' };
ajax_success(response);

ok(callback_invoked, "Should invoke success callback");
same(model.attributes().foo, 'fooism', "Should have updated attributes");
equals(model.html(), "html", "Should have updated html of the model.");
});
});

0 comments on commit fd554b8

Please sign in to comment.