Skip to content

Commit

Permalink
Make path-prefixed destroy work for instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Nat Budin committed May 2, 2008
1 parent cc286ba commit 1bf618a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
41 changes: 31 additions & 10 deletions jester.js
Expand Up @@ -490,27 +490,48 @@ Object.extend(Jester.Resource.prototype, {
return this;
},

// If not given an ID, destroys itself, if it has an ID. If given an ID, destroys that record.
// You can call destroy(), destroy(1), destroy(callback()), or destroy(1, callback()), and it works as you expect.
destroy : function(given_id, callback) {
if (typeof(given_id) == "function") {
callback = given_id;
given_id = null;
// Destroys a REST object. Can be used as follows:
// object.destroy() - when called on an instance of a model, destroys that instance
// Model.destroy(1) - destroys the Model object with ID 1
// Model.destroy({parent: 3, id: 1}) - destroys the Model object with Parent ID 3 and ID 1
//
// Any of these forms can also be passed a callback function as an additional parameter and it works as you expect.
destroy : function(params, callback) {
if (params === undefined) {
params = {};
}
if (typeof(params) == "function") {
callback = params;
params = {};
}
if (typeof(params) == "number") {
params = {id: params};
}
if (!params.id) {
params.id = this.id;
}
if (!params.id) return false;

// collect params from instance if we're being called as an instance method
if (this._properties !== undefined) {
(this._properties).each( bind(this, function(value, i) {
if (params[value] === undefined) {
params[value] = this[value];
}
}));
}
var id = given_id || this.id;
if (!id) return false;

var destroyWork = bind(this, function(transport) {
if (transport.status == 200) {
if (!given_id || this.id == given_id)
if (!params.id || this.id == params.id)
this.id = null;
return this;
}
else
return false;
});

return this.klass.request(destroyWork, this._destroy_url(), {method: "delete"}, callback);
return this.klass.request(destroyWork, this._destroy_url(params), {method: "delete"}, callback);
},

save : function(callback) {
Expand Down
9 changes: 4 additions & 5 deletions test/jester_test.html
Expand Up @@ -446,7 +446,7 @@
********************************************/

function testDestroy() {
var eric = User.build({id: 1})
/*var eric = User.build({id: 1})
Ajax = { Request : function (url, options) {
this.transport = { status : 500 };
}
Expand All @@ -467,16 +467,15 @@
assertEquals(eric, eric.destroy());
assertNull(eric.id);
assert_changed();
});
});*/

// Path prefixed destroy
Resource.model("Post", {prefix: "/users/:user_id"});

var eric = User.build({id: 1})
var post = Post.build({id: 3, user: eric});
var post = Post.build({id: 3, user_id: 1});
var uriRegEx = RegExp("/users/1/posts/3");
Ajax = { Request : function (url, options) {
assert("URL passed ("+url+") does not match expected path: "+uriRegEx,
assert("URL passed ("+url+") does not match expected path: "+uriRegEx,
uriRegEx.test(url));
this.transport = { status: 200 };
}
Expand Down

0 comments on commit 1bf618a

Please sign in to comment.