can.Model
can.Model([name,] staticProperties, instanceProperties)
Create a can.Model constructor. (See can.Construct for more details on this syntax.)
Parameters
-
name
{String}
OptionalIf given, this will be the globally-available name of the constructor function.
-
staticProperties
{Object}
The static properties of the class. See below for properties with special meanings to
can.Model
. -
instanceProperties
{Object}
The instance properties of instances of the class. These will usually be functions.
Returns
{function()}
A can.Model constructor.
new can.Model([options])
Creates a new instance of ModelConstructor.
Parameters
-
options
{Object}
OptionalOptions to pass to
setup
orinit
.
Returns
{can.Model}
A new instance of ModelConstructor.
Model adds service encapsulation to can.Map. Model lets you:
Get and modify data from the server
can.Model makes connecting to a JSON REST service really easy. The following models
todos
by describing the services that can create, retrieve, update, and delete todos.This lets you create, retrieve, update, and delete todos programatically:
Create
Create a todo instance and call
save(success, error)
to create the todo on the server.Retrieve
Retrieve a list of todos from the server with
findAll(params, success(items), error)
:Retrieve a single todo from the server with
findOne(params, success(item), error)
:Update
Once an item has been created on the server, you can change its properties and call
save
to update it on the server.Destroy
Call
destroy(success, error)
to delete an item on the server.Listen to changes in data
Listening to changes in data is a critical part of the Model-View-Controller architecture. can.Model lets you listen to when an item is created, updated, destroyed or its properties are changed. Use
Model.bind(eventType, handler(event, model))
to listen to all events of type on a model andmodel.bind(eventType, handler(event))
to listen to events on a specific instance.Create
Update
Destroy
Property Changes
Listening with can.Control or can.Component
You can use can.Control or the events property of can.Component to listen to model changes like: