resource

  • property
can.Model.resource

{String}

 

Define a restful resource URL.

String

A string URL to a restful resource. If the resource is specified as "resource" and the model's id is "id", resource will implement can.Model's ajax methods as follows:

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

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}"
}, {});