can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
65 lines (49 loc) • 1.59 kB
Markdown
@property {String} can.Model.resource resource
@parent can.Model.static
@description Define a restful resource URL.
@option {String}
A string URL to a restful resource. If the resource
is specified as `"resource"` and the model's [can.Model.id id] is
`"id"`, resource will implement [can.Model]'s ajax methods as follows:
- [can.Model.findAll] - `"GET resource"`
- [can.Model.findOne] - `"GET resource/{id}"`
- [can.Model.create] - `"POST resource"`
- [can.Model.update] - `"PUT resource/{id}"`
- [can.Model.destroy] - `"DELETE resource/{id}"`
Setting the `resource` property will not overwrite other implemented
ajax methods, however will overwrite inherited ajax methods.
@body
## Use
For each of the names (create, update, destroy, findOne, and findAll) use the
URL provided by the `resource` property. For example:
```
Todo = can.Model.extend({
resource: "/todos"
}, {});
```
Will create a can.Model that is identical to:
```
Todo = can.Model.extend({
findAll: "GET /todos",
findOne: "GET /todos/{id}",
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}"
}, {});
```