屬性和觀察可能性 - Attributes and Observables
model層一個絕對的重要性在於存取物件資料的屬性(property), 和監聽model個體的改變。這就是觀察者模式而且正是MVC的重要環節 - view監聽model的改變。
- offset - 要顯示的第一個項目的位置
- limit - 總共要顯示多少項目
- count - 全部項目的數量
The paginate variable is now observable. We can pass it to pagination controls that can read from, write to, and listen for property changes. You can read properties like normal or using the
If we clicked the next button, we need to increment the offset. Change property values with
model.attr(NAME, VALUE). The following moves the offset to the next page:
When paginate's state is changed by one control, the other controls need to be notified. You can bind to a specific attribute change with
model.bind(ATTR, success( ev, newVal ) ) and update the control:
You can also listen to any attribute change by binding to the
The following is a next-previous jQuery plugin that accepts paginate data:
There are a few problems with this plugin. First, if the control is removed from the page, it is not unbinding itself from paginate. We'll address this when we discuss controllers.
Second, the logic protecting a negative offset or offset above the total count is done in the plugin. This logic should be done in the model. To fix this problem, we'll need to add additional constraints to limit what values limit, offset, and count can be. We'll need to create a pagination class.
$.Model(NAME, [STATIC,] PROTOTYPE):
There are a few ways to make the Paginate model more useful. First, by adding setter methods, we can limit what values count and offset can be set to.
Settter methods are model prototype methods that are named
setNAME. They get called with the val passed to
model.attr(NAME, val) and a success and error callback. Typically, the method should return the value that should be set on the model instance or call error with an error message. Success is used for asynchronous setters.
The following paginate model uses setters to prevent negative counts the offset from exceeding the count by adding
setOffset instance methods.
Now the nextPrev plugin can set offset with reckless abandon:
We can add default values to Paginate instances by setting the static
defaults property. When a new paginate instance is created, if no value is provided, it initializes with the default value.
This is getting sexy, but the Paginate model can make it even easier to move to the next and previous page and know if it's possible by adding helper methods.
Helper methods are prototype methods that help set or get useful data on model instances. The following, completed, Paginate model includes a
prev method that will move to the next and previous page if possible. It also provides a
canPrev method that returns if the instance can move to the next page or not.
Thus, our jQuery widget becomes much more refined:
We've just seen how $.Model is useful for modeling client side state. However, for most applications, the critical data is on the server, not on the client. The client needs to create, retrieve, update and delete (CRUD) data on the server. Maintaining the duality of data on the client and server is tricky business. $.Model is used to simplify this problem.
$.Model is extremely flexible. It can be made to work with all sorts of services types and data types. This book covers only how $.Model works with the most common and popular type of service and data type: Representational State Transfer (REST) and JSON.
A REST service uses urls and the HTTP verbs POST, GET, PUT, DELETE to create, retrieve, update, and delete data respectively. For example, a tasks service that allowed you to create, retrieve, update and delete tasks might look like:
|Create a task||POST||/tasks||name=do the dishes||
|Get a task||GET||/task/2||
|Update a task||PUT||/task/2||name=take out recycling||
|Delete a task||DELETE||/task/2||
TODO: We can label the urls
The following connects to task services, letting us create, retrieve, update and delete tasks from the server:
The following table details how to use the task model to CRUD tasks.
|Create a task||
To create an instance of a model on the server, first create an instance with
Save checks if the task has an id. In this case it does not so save makes a create request with the task's attributes. Save takes two parameters:
|Get a task||
||Retrieves a single task from the server. It takes three parameters:
||Get tasksrst change the attributes of a model instance with|
|Destroy a task||
Destroys a task on the server. Destroy takes two parameters:
Did you notice how the server responded with createdAt values as numbers like
1303173531164. This number is actually April 18th, 2011. Instead of getting a number back from
new Date(1303173531164). We could do this with a
setCreatedAt setter. But, if we have lots of date types, this will quickly get repetitive.
To make this easy, $.Model lets you define the type of an attribute and a converter function for those types. Set the type of attributes on the static
attributes object and converter methods on the static
Task now converts createdAt to a Date type. To list the year of each task, write:
Model publishes events when an instance has been created, updated, or destroyed. You can listen to these events globally on the Model or on an individual model instance. Use
MODEL.bind(EVENT, callback( ev, instance ) ) to listen for created, updated, or destroyed events.
Lets say we wanted to know when a task is created and add it to the page. After it's been added to the page, we'll listen for updates on that task to make sure we are showing its name correctly. We can do that like: