Permalink
Browse files

Version 0.8.4.

  • Loading branch information...
1 parent cdb1578 commit c33ed77ef748afe32446bdea70172c487f266654 @benpickles committed Jul 2, 2010
Showing with 41 additions and 10 deletions.
  1. +4 −0 CHANGELOG.markdown
  2. +14 −1 README.markdown
  3. +16 −2 dist/js-model-0.8.3.js
  4. +7 −7 dist/js-model-0.8.3.min.js
View
@@ -1,5 +1,9 @@
# Changelog
+## 0.8.4
+
+* Add named parameters to `Model.RestPersistence` so "/categories/:category_id/posts" will pick up the corresponding `category_id` attribute from your model and be converted to "/categories/2/posts". [Russell Jones]
+
## 0.8.3
* Fix that `find`, `first`, `last` and `detect` return `null` when nothing is found.
View
@@ -101,7 +101,10 @@ Setting up persistence for a given model is handled when the class is created:
Calling __save__ or __destroy__ on an object now fires a corresponding REST Request:
- var post = new Post({foo: 'bar'}).save() // makes a POST request to '/posts'
+ var post = new Post({foo: 'bar'})
+ post.save() // POST /posts
+ post.attr("foo", "baz").save() // PUT /posts/1
+ post.destroy() // DELETE /posts/1
A custom callback can be fed into the save method to execute on success or failure:
@@ -115,6 +118,16 @@ A custom callback can be fed into the save method to execute on success or failu
The default triggered events (mentioned earlier) are generally only called once persistence is successful.
+You can also include named parameters in the URL that will pick up the corresponding values from the model instance:
+
+ var Post = Model("post", {
+ persistence: Model.RestPersistence("/categories/:category_id/posts")
+ }
+ var post = new Post({ title: "Foo", category_id: 2 })
+
+ // POST request made to /categories/2/posts
+ post.save()
+
## Validations
It is possible to have local validations to avoid hitting your server unnecessarily.
View
@@ -301,17 +301,31 @@ Model.Log = function() {
if (window.console) window.console.log.apply(window.console, arguments);
};
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) {
@@ -350,7 +364,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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit c33ed77

Please sign in to comment.