Skip to content

Commit

Permalink
additional docs for #1089
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Abril committed Jul 1, 2014
1 parent 241cb81 commit c314524
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 18 additions & 2 deletions model/doc/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ is specified as `"resource"` and the model's [can.Model.id id] is
- [can.Model.destroy] - `"DELETE resource/{id}"`

Setting the `resource` property will not overwrite other implemented
ajax methods.
ajax methods, however will overwrite inherited ajax methods.

@body

Expand All @@ -37,4 +37,20 @@ Will create a can.Model that is identical to:
create: "POST /todos",
update: "PUT /todos/{id}",
destroy: "DELETE /todos/{id}"
},{});
},{});

Inherited AJAX methods will be overwritten when using the `resource` property. For example, inheriting our Todo model:

SpecialTodo = Todo.extend({
resource: "/specialTodos"
}, {});

Will create a Todo model identical to:

SpecialTodo = can.Model.extend({
findAll: "GET /specialTodos",
findOne: "GET /specialTodos/{id}",
create: "POST /specialTodos",
update: "PUT /specialTodos/{id}",
destroy: "DELETE /specialTodos/{id}"
}, {});
9 changes: 8 additions & 1 deletion model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1183,13 +1183,20 @@ steal('can/util', 'can/map', 'can/list', function (can) {

// Go through `ajaxMethods` and set up static methods according to their configurations.
can.each(ajaxMethods, function (method, name) {
//TODO: Docs
// Check the configuration for this ajaxMethod.
// If the configuration isn't a function, it should be a string (like `"GET /endpoint"`)
// or an object like `{url: "/endpoint", type: 'GET'}`.

//if we have a string(like `"GET /endpoint"`) set in the static definition(not inherited), convert it
//to a function.
if(staticProps && staticProps[name] && typeof staticProps[name] === 'string') {
self[name] = ajaxMaker(method, staticProps[name]);
}
//if we have a resource property set in the static definition
else if(staticProps && staticProps.resource) {
self[name] = ajaxMaker(method, createURLFromResource(self, name));
}
//finally, if we have an object definition(static or inherited)
else if(!can.isFunction(self[name])) {
self[name] = ajaxMaker(method, self[name]);
}
Expand Down

0 comments on commit c314524

Please sign in to comment.