Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make path-prefixed destroy work for instances

  • Loading branch information...
commit 1bf618a87ca7e03d2d6e92017aa96319326bcb14 1 parent cc286ba
Nat Budin authored
Showing with 35 additions and 15 deletions.
  1. +31 −10 jester.js
  2. +4 −5 test/jester_test.html
View
41 jester.js
@@ -490,19 +490,40 @@ 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;
}
@@ -510,7 +531,7 @@ Object.extend(Jester.Resource.prototype, {
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) {
View
9 test/jester_test.html
@@ -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 };
}
@@ -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 };
}
Please sign in to comment.
Something went wrong with that request. Please try again.