Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

addition of named params for resource paths in Model.RestPersistence

  • Loading branch information...
commit cdb15786d234665ed11243b751eef07eab34fb6c 1 parent 86772ac
@CodeOfficer authored
View
18 src/model_rest_persistence.js
@@ -1,15 +1,29 @@
Model.RestPersistence = function(resource, methods) {
+ var PARAM_NAME_MATCHER = /:([\w\d]+)/g;
+
var model_resource = function() {
this.resource = resource;
+ this.resource_param_names = [];
+ while ((param_name = PARAM_NAME_MATCHER.exec(resource)) !== null) {
+ this.resource_param_names.push(param_name[1]);
+ };
};
jQuery.extend(model_resource.prototype, {
+ path: function(model) {
+ var path = this.resource;
+ $.each(this.resource_param_names, function(i, param) {
+ path = path.replace(":" + param, model.attributes[param]);
+ });
+ return path;
+ },
+
create: function(model, callback) {
return this.xhr('POST', this.create_path(model), model, callback);
},
create_path: function(model) {
- return this.resource;
+ return this.path(model);
},
destroy: function(model, callback) {
@@ -48,7 +62,7 @@ Model.RestPersistence = function(resource, methods) {
},
update_path: function(model) {
- return [this.resource, model.id()].join('/');
+ return [this.path(model), model.id()].join('/');
},
xhr: function(method, url, model, callback) {
View
11 test/app.rb
@@ -11,13 +11,24 @@
JSON.generate({ :id => 1, :title => 'Foo amended', :body => '...', :foo => 'bar' })
end
+post '/root/:root_id/nested/:nested_id/posts' do
+ JSON.generate({ :id => 1, :title => 'Nested created', :body => '...', :root_id => 3, :nested_id => 2 })
+end
+
put '/posts/:id' do
JSON.generate({ :id => 1, :title => 'Bar amended', :body => '...' })
end
+put '/root/:root_id/nested/:nested_id/posts/:id' do
+ JSON.generate({ :id => 1, :title => 'Nested amended', :body => '...', :root_id => 3, :nested_id => 2 })
+end
+
delete '/posts/:id' do
end
+delete '/root/:root_id/nested/:nested_id/posts/:id' do
+end
+
# Blank response (Rails' `head :ok`)
put '/posts-empty-response/:id' do
' '
View
68 test/public/tests/model_rest_persistence.js
@@ -1,5 +1,73 @@
module("Model.RestPersistence");
+test("create with named params in resource path", function() {
+ var Post = Model("post", {
+ persistence: Model.RestPersistence("/root/:root_id/nested/:nested_id/posts")
+ });
+ var post = new Post({ title: "Nested", body: "...", root_id: 3, nested_id: 2 });
+
+ AjaxSpy.start();
+ stop();
+
+ post.save(function(success) {
+ ok(success);
+ start();
+ });
+
+ equals(AjaxSpy.requests.length, 1, "one request should have been made");
+
+ var request = AjaxSpy.requests.shift();
+
+ equals(request.type, "POST");
+ equals(request.url, "/root/3/nested/2/posts");
+});
+
+test("update with named params in resource path", function() {
+ var Post = Model("post", {
+ persistence: Model.RestPersistence("/root/:root_id/nested/:nested_id/posts")
+ });
+ var post = new Post({ id: 1, title: "Nested", body: "...", root_id: 3, nested_id: 2 });
+ post.attr("title", "Nested amended");
+
+ AjaxSpy.start();
+ stop();
+
+ post.save(function(success) {
+ ok(success);
+ start();
+ });
+
+ equals(AjaxSpy.requests.length, 1, "one request should have been made");
+
+ var request = AjaxSpy.requests.shift();
+
+ equals(request.type, "PUT");
+ equals(request.url, "/root/3/nested/2/posts/1");
+});
+
+test("destroy with named params in resource path", function() {
+ var Post = Model("post", {
+ persistence: Model.RestPersistence("/root/:root_id/nested/:nested_id/posts")
+ });
+ var post = new Post({ id: 1, title: "Nested", body: "...", root_id: 3, nested_id: 2 });
+
+ AjaxSpy.start();
+ stop();
+
+ post.destroy(function(success) {
+ ok(success);
+ start();
+ });
+
+ equals(AjaxSpy.requests.length, 1, "one request should have been made");
+
+ var request = AjaxSpy.requests.shift();
+
+ equals(request.type, "DELETE");
+ equals(request.url, "/root/3/nested/2/posts/1");
+ same(request.data, null);
+});
+
test("create", function() {
var Post = Model("post", {
persistence: Model.RestPersistence("/posts")
Please sign in to comment.
Something went wrong with that request. Please try again.