~ubuntu-branches/ubuntu/precise/whoopsie-daisy/precise-updates

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/docs/model/index.html

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-04-18 13:04:36 UTC
  • Revision ID: package-import@ubuntu.com-20120418130436-vmt93p8fds516lws
Tags: 0.1.32
Fix failing tests on powerpc and ARM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html>
 
2
<html lang="en">
 
3
<head>
 
4
    <meta charset="utf-8">
 
5
    <title>Model</title>
 
6
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
 
7
    <link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
 
8
    <link rel="stylesheet" href="../assets/css/main.css">
 
9
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
 
10
    <script src="../../build/yui/yui-min.js"></script>
 
11
</head>
 
12
<body>
 
13
 
 
14
<div id="doc">
 
15
    <h1>Model</h1>
 
16
 
 
17
    
 
18
        <a href="#toc" class="jump">Jump to Table of Contents</a>
 
19
    
 
20
 
 
21
    <div class="yui3-g">
 
22
        <div class="yui3-u-3-4">
 
23
            <div id="main">
 
24
                <div class="content"><div class="intro">
 
25
<p>
 
26
Model is a lightweight <a href="../attribute/index.html">Attribute</a>-based data model with methods for getting, setting, validating, and syncing attribute values to a persistence layer or server, as well as events for notifying listeners of model changes.
 
27
</p>
 
28
 
 
29
<p>
 
30
The <code>Y.Model</code> class is intended to be extended by a custom class that defines
 
31
custom model attributes, validators, and behaviors.
 
32
</p>
 
33
</div>
 
34
 
 
35
<h2 id="getting-started">Getting Started</h2>
 
36
 
 
37
<p>
 
38
To include the source files for Model and its dependencies, first load
 
39
the YUI seed file if you haven't already loaded it.
 
40
</p>
 
41
 
 
42
<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.5.0&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>
 
43
 
 
44
 
 
45
<p>
 
46
Next, create a new YUI instance for your application and populate it with the
 
47
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
 
48
YUI will automatically load any dependencies required by the modules you
 
49
specify.
 
50
</p>
 
51
 
 
52
<pre class="code prettyprint">&lt;script&gt;
 
53
&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
 
54
YUI().use(&#x27;model&#x27;, function (Y) {
 
55
    &#x2F;&#x2F; Model is available and ready for use. Add implementation
 
56
    &#x2F;&#x2F; code here.
 
57
});
 
58
&lt;&#x2F;script&gt;</pre>
 
59
 
 
60
 
 
61
<p>
 
62
For more information on creating YUI instances and on the
 
63
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
 
64
documentation for the <a href="../yui/index.html">YUI Global Object</a>.
 
65
</p>
 
66
 
 
67
 
 
68
<h2 id="what-is-a-model">What is a Model?</h2>
 
69
 
 
70
<p>
 
71
A model is a class that manages data, state, and behavior associated with an application or a part of an application.
 
72
</p>
 
73
 
 
74
<p>
 
75
For example, in a photo gallery, you might use a model to represent each photo. The model would contain information about the image file, a caption, tags, etc., along with methods for working with this data. The model would also be responsible for validating any new data before accepting it.
 
76
</p>
 
77
 
 
78
<p>
 
79
While Model may be used as a standalone component, it's common to associate a Model instance with a <a href="../view/index.html">View</a> instance, which is responsible for rendering the visual representation of the data contained in the model and updating that representation when the model changes.
 
80
</p>
 
81
 
 
82
<h2 id="using-model">Using Model</h2>
 
83
 
 
84
<h3 id="quick-start">Quick Start</h3>
 
85
 
 
86
<p>
 
87
The quickest way to get up and running with Model is to create a new instance of the <code>Y.Model</code> class and pass in an object of key/value pairs representing attributes to set (note: creating ad-hoc attributes like this requires YUI 3.5.0 or higher).
 
88
</p>
 
89
 
 
90
<p>
 
91
Here's how you might create a simple model representing a delicious pie (imagine you're building an online ordering system for a bakery):
 
92
</p>
 
93
 
 
94
<pre class="code prettyprint">var applePie = new Y.Model({
 
95
  slices: 6,       &#x2F;&#x2F; number of slices in this pie
 
96
  type  : &#x27;apple&#x27;  &#x2F;&#x2F; what type of pie this is
 
97
});</pre>
 
98
 
 
99
 
 
100
<p>
 
101
This creates a new <code>Y.Model</code> instance named <code>applePie</code>, with two attributes: <code>slices</code> and <code>type</code>, in addition to the <a href="#built-in-attributes">built-in attributes</a> <code>id</code> and <code>clientId</code>, which are available on all models.
 
102
</p>
 
103
 
 
104
<p>
 
105
Pass the name of an attribute to the model's <code>get()</code> function to get that attribute's value.
 
106
</p>
 
107
 
 
108
<pre class="code prettyprint">applePie.get(&#x27;slices&#x27;); &#x2F;&#x2F; =&gt; 6
 
109
applePie.get(&#x27;type&#x27;); &#x2F;&#x2F; =&gt; &quot;apple&quot;</pre>
 
110
 
 
111
 
 
112
<p>
 
113
To change the value of an attribute, pass its name and new value to <code>set()</code>.
 
114
</p>
 
115
 
 
116
<pre class="code prettyprint">applePie.set(&#x27;slices&#x27;, 5); &#x2F;&#x2F; someone ate a slice!</pre>
 
117
 
 
118
 
 
119
<p>
 
120
Read on to learn how to create custom model subclasses for representing more complex data and logic, or skip ahead to <a href="#model-events">Model Events</a> or <a href="#getting-and-setting-attribute-values">Getting and Setting Attribute Values</a> to learn more about how to take advantage of the useful state management functionality Model provides.
 
121
</p>
 
122
 
 
123
<h3 id="extending-ymodel">Extending <code>Y.Model</code></h3>
 
124
 
 
125
<p>
 
126
The ability to create ad-hoc models by instantiating <code>Y.Model</code> is convenient, but sometimes it can be useful to create a custom Model subclass by <em>extending</em> <code>Y.Model</code>. This allows you to declare the data attributes your Model class will manage up front, as well as specify default attribute values, helper methods, validators, and (optionally) a sync layer to help your Model class communicate with a storage API or a remote server.
 
127
</p>
 
128
 
 
129
<p>
 
130
In this example, we'll create a <code>Y.PieModel</code> class. Each instance of this class will represent a delicious pie, fresh from the oven.
 
131
</p>
 
132
 
 
133
<pre class="code prettyprint">&#x2F;&#x2F; Create a new Y.PieModel class that extends Y.Model.
 
134
Y.PieModel = Y.Base.create(&#x27;pieModel&#x27;, Y.Model, [], {
 
135
  &#x2F;&#x2F; Add prototype methods for your Model here if desired. These methods will be
 
136
  &#x2F;&#x2F; available to all instances of your Model.
 
137
 
 
138
  &#x2F;&#x2F; Returns true if all the slices of the pie have been eaten.
 
139
  allGone: function () {
 
140
    return this.get(&#x27;slices&#x27;) === 0;
 
141
  },
 
142
 
 
143
  &#x2F;&#x2F; Consumes a slice of pie, or fires an &#x60;error&#x60; event if there are no slices
 
144
  &#x2F;&#x2F; left.
 
145
  eatSlice: function () {
 
146
    if (this.allGone()) {
 
147
      this.fire(&#x27;error&#x27;, {
 
148
        type : &#x27;eat&#x27;,
 
149
        error: &quot;Oh snap! There isn&#x27;t any pie left.&quot;
 
150
      });
 
151
    } else {
 
152
      this.set(&#x27;slices&#x27;, this.get(&#x27;slices&#x27;) - 1);
 
153
      Y.log(&#x27;You just ate a slice of delicious &#x27; + this.get(&#x27;type&#x27;) + &#x27; pie!&#x27;);
 
154
    }
 
155
  }
 
156
}, {
 
157
  ATTRS: {
 
158
    &#x2F;&#x2F; Add custom model attributes here. These attributes will contain your
 
159
    &#x2F;&#x2F; model&#x27;s data. See the docs for Y.Attribute to learn more about defining
 
160
    &#x2F;&#x2F; attributes.
 
161
 
 
162
    slices: {
 
163
      value: 6 &#x2F;&#x2F; default value
 
164
    },
 
165
 
 
166
    type: {
 
167
      value: &#x27;apple&#x27;
 
168
    }
 
169
  }
 
170
});</pre>
 
171
 
 
172
 
 
173
<p>
 
174
Now we can create instances of <code>Y.PieModel</code> to represent delicious pies.
 
175
</p>
 
176
 
 
177
<p>
 
178
Each instance will have a <code>type</code> attribute containing the type of the pie and a <code>slices</code> attribute containing the number of slices remaining. We can call the <code>allGone()</code> method to check whether there are any slices left, and the <code>eatSlice()</code> method to eat a slice.
 
179
</p>
 
180
 
 
181
<pre class="code prettyprint">&#x2F;&#x2F; Bake a delicious new pecan pie.
 
182
var pecanPie = new Y.PieModel({type: &#x27;pecan&#x27;});
 
183
 
 
184
pecanPie.on(&#x27;error&#x27;, function (e) {
 
185
  Y.log(e.error);
 
186
});
 
187
 
 
188
pecanPie.eatSlice(); &#x2F;&#x2F; =&gt; &quot;You just ate a slice of delicious pecan pie!&quot;
 
189
Y.log(pecanPie.get(&#x27;slices&#x27;)); &#x2F;&#x2F; =&gt; 5</pre>
 
190
 
 
191
 
 
192
<p>
 
193
Five slices later, our pie will be all gone. If we try to eat another slice, we'll get an <code>error</code> event.
 
194
</p>
 
195
 
 
196
<pre class="code prettyprint">&#x2F;&#x2F; 5 slices later...
 
197
pecanPie.eatSlice(); &#x2F;&#x2F; =&gt; &quot;Oh snap! There isn&#x27;t any pie left.&quot;</pre>
 
198
 
 
199
 
 
200
<h3 id="model-attributes">Model Attributes</h3>
 
201
 
 
202
<p>
 
203
A Model's data is represented by <a href="../attribute/index.html">attributes</a>. The Model class provides two built-in attributes, <code>clientId</code> and <code>id</code>. The rest are up to you to define when you extend <code>Y.Model</code>.
 
204
</p>
 
205
 
 
206
<h4 id="built-in-attributes">Built-in Attributes</h4>
 
207
 
 
208
<table>
 
209
  <thead>
 
210
    <tr>
 
211
      <th>Attribute</th>
 
212
      <th>Default Value</th>
 
213
      <th>Description</th>
 
214
    </tr>
 
215
  </thead>
 
216
  <tbody>
 
217
    <tr>
 
218
      <td><code>clientId</code></td>
 
219
      <td><em>generated id</em></td>
 
220
      <td>
 
221
        <p>
 
222
        An automatically generated client-side only unique ID for identifying model instances that don't yet have an <code>id</code>. The client id is guaranteed to be unique among all models on the current page, but is not unique across pageviews. Client ids are never included in <code>toJSON()</code> output.
 
223
        </p>
 
224
      </td>
 
225
    </tr>
 
226
 
 
227
    <tr>
 
228
      <td><code>id</code></td>
 
229
      <td><code>null</code></td>
 
230
      <td>
 
231
        <p>
 
232
        A globally unique identifier for the model instance. If this is <code>null</code>, then the model instance is assumed to be "new" (meaning it hasn't yet been saved to a persistence layer). If the model represents data that's stored in a database of some kind, it usually makes sense to use the database's primary key here.
 
233
        </p>
 
234
 
 
235
        <p>
 
236
        If your persistence layer uses a primary key with a name other than <code>id</code>, you can override the <code>idAttribute</code> property and set it to the name of your custom id attribute when extending <code>Y.Model</code> (be sure to define a corresponding attribute as well). The <code>id</code> attribute will then act as an alias for your custom id attribute.
 
237
        </p>
 
238
      </td>
 
239
    </tr>
 
240
  </tbody>
 
241
</table>
 
242
 
 
243
<h4 id="custom-attributes">Custom Attributes</h4>
 
244
 
 
245
<p>
 
246
Custom attributes are where all your model's data should live. You define these attributes when you first extend <code>Y.Model</code>. You can set their values by passing a config object into the model's constructor, or by calling the model's <code>set()</code> or <code>setAttrs()</code> methods..
 
247
</p>
 
248
 
 
249
<p>
 
250
Attributes can specify default values, getters and setters, validators, and more. For details, see the documentation for the <a href="../attribute/index.html">Attribute</a> component.
 
251
</p>
 
252
 
 
253
<h3 id="model-properties">Model Properties</h3>
 
254
 
 
255
<p>
 
256
The following properties are available on Model instances. The <code>idAttribute</code> property may be overridden when extending <code>Y.Model</code>; the others are intended to be read-only.
 
257
</p>
 
258
 
 
259
<table>
 
260
  <thead>
 
261
    <tr>
 
262
      <th>Property</th>
 
263
      <th>Type</th>
 
264
      <th>Description</th>
 
265
    </tr>
 
266
  </thead>
 
267
  <tbody>
 
268
    <tr>
 
269
      <td><code>changed</code></td>
 
270
      <td>Object</td>
 
271
      <td>
 
272
        <p>
 
273
        Hash of attributes that have changed since the last time the model was saved.
 
274
        </p>
 
275
      </td>
 
276
    </tr>
 
277
 
 
278
    <tr>
 
279
      <td><code>idAttribute</code></td>
 
280
      <td>String</td>
 
281
      <td>
 
282
        <p>
 
283
        Name of the attribute to use as the unique id for the model. Defaults to "id", but you may override this property when extending <code>Y.Model</code> if you want to specify a custom id attribute.
 
284
        </p>
 
285
      </td>
 
286
    </tr>
 
287
 
 
288
    <tr>
 
289
      <td><code>lastChange</code></td>
 
290
      <td>Object</td>
 
291
      <td>
 
292
        <p>
 
293
        Hash of attributes that were changed in the most recent <code>change</code> event. Each item in this hash is an object with the following properties:
 
294
        </p>
 
295
 
 
296
        <dl style="margin-top: 1em;">
 
297
          <dt><strong><code>newVal</code></strong></dt>
 
298
          <dd>
 
299
            <p>
 
300
            The new value of the attribute after it changed.
 
301
            </p>
 
302
          </dd>
 
303
 
 
304
          <dt><strong><code>prevVal</code></strong></dt>
 
305
          <dd>
 
306
            <p>
 
307
            The old value of the attribute before it changed.
 
308
            </p>
 
309
          </dd>
 
310
 
 
311
          <dt><strong><code>src</code></strong></dt>
 
312
          <dd>
 
313
            <p>
 
314
            The source of the change, or <code>null</code> if no source was specified when the change was made.
 
315
            </p>
 
316
 
 
317
            <p>
 
318
            This can be set to any string you want by passing an options object like <code>{src: &#x27;foo&#x27;}</code> to the <code>set()</code> or <code>setAttrs()</code> methods when changing attribute values. Its purpose is to allow you to identify the source of the change later by inspecting the <code>src</code> property associated with an event, so choose a string that has meaning for your particular use case.
 
319
            </p>
 
320
          </dd>
 
321
        </dl>
 
322
      </td>
 
323
    </tr>
 
324
 
 
325
    <tr>
 
326
      <td><code>lists</code></td>
 
327
      <td>Array</td>
 
328
      <td>
 
329
        <p>
 
330
        Array of <a href="../model-list/index.html">ModelList</a> instances that contain this model.
 
331
        </p>
 
332
 
 
333
        <p>
 
334
        This property is updated automatically when the model is added to or removed from a ModelList instance. You shouldn't alter it manually. When working with models in a list, you should always add and remove models using the list's <code>add()</code> and <code>remove()</code> methods.
 
335
        </p>
 
336
      </td>
 
337
    </tr>
 
338
  </tbody>
 
339
</table>
 
340
 
 
341
<h3 id="model-events">Model Events</h3>
 
342
 
 
343
<p>
 
344
Model instances provide the following events:
 
345
</p>
 
346
 
 
347
<table>
 
348
  <thead>
 
349
    <tr>
 
350
      <th>Event</th>
 
351
      <th>When</th>
 
352
      <th>Payload</th>
 
353
    </tr>
 
354
  </thead>
 
355
  <tbody>
 
356
    <tr>
 
357
      <td><code>change</code></td>
 
358
      <td>
 
359
        <p>
 
360
        One or more attributes on the model are changed.
 
361
        </p>
 
362
      </td>
 
363
      <td>
 
364
        <dl>
 
365
          <dt><code>changed</code> (<em>Object</em>)</dt>
 
366
          <dd>
 
367
            <p>
 
368
            Hash of change information for each attribute that changed. Keys are attribute names, values are objects with the following properties:
 
369
            </p>
 
370
 
 
371
            <dl style="margin-top: 1em;">
 
372
              <dt><code>newVal</code></dt>
 
373
              <dd>
 
374
                <p>
 
375
                The new value of the attribute after it changed.
 
376
                </p>
 
377
              </dd>
 
378
 
 
379
              <dt><code>prevVal</code></dt>
 
380
              <dd>
 
381
                <p>
 
382
                The old value of the attribute before it changed.
 
383
                </p>
 
384
              </dd>
 
385
 
 
386
              <dt><code>src</code></dt>
 
387
              <dd>
 
388
                <p>
 
389
                The source of the change, or <code>null</code> if no source was specified when the change was made.
 
390
                </p>
 
391
 
 
392
                <p>
 
393
                This can be set to any string you want by passing an options object like <code>{src: &#x27;foo&#x27;}</code> to the <code>set()</code> or <code>setAttrs()</code> methods when changing attribute values. Its purpose is to allow you to identify the source of the change later by inspecting the <code>src</code> property associated with an event, so choose a string that has meaning for your particular use case.
 
394
                </p>
 
395
              </dd>
 
396
            </dl>
 
397
          </dd>
 
398
        </dl>
 
399
      </td>
 
400
    </tr>
 
401
 
 
402
    <tr>
 
403
      <td><code>error</code></td>
 
404
      <td>
 
405
        <p>
 
406
        An error occurs, such as when the model fails validation or a sync layer response can't be parsed.
 
407
        </p>
 
408
      </td>
 
409
      <td>
 
410
        <dl>
 
411
          <dt><code>error</code></dt>
 
412
          <dd>
 
413
            <p>
 
414
              Error message, object, or exception generated by the error. Calling <code>toString()</code> on this should result in a meaningful error message.
 
415
            </p>
 
416
          </dd>
 
417
 
 
418
          <dt><code>src</code></dt>
 
419
          <dd>
 
420
            <p>
 
421
            Source of the error. May be one of the following default sources, or any custom error source used by your Model subclass):
 
422
            </p>
 
423
 
 
424
            <dl style="margin-top: 1em;">
 
425
              <dt><code>load</code></dt>
 
426
              <dd>
 
427
                <p>
 
428
                An error loading the model from a sync layer. The sync layer's response (if any) will be provided as the <code>response</code> property on the event facade.
 
429
                </p>
 
430
              </dd>
 
431
 
 
432
              <dt><code>parse</code></dt>
 
433
              <dd>
 
434
                <p>
 
435
                An error parsing a response from a sync layer.
 
436
                </p>
 
437
              </dd>
 
438
 
 
439
              <dt><code>save</code></dt>
 
440
              <dd>
 
441
                <p>
 
442
                An error saving the model to a sync layer. The sync layer's response (if any) will be provided as the <code>response</code> property on the event facade.
 
443
                </p>
 
444
              </dd>
 
445
 
 
446
              <dt><code>validate</code></dt>
 
447
              <dd>
 
448
                <p>
 
449
                The model failed to validate.
 
450
                </p>
 
451
              </dd>
 
452
            </dl>
 
453
          </dd>
 
454
        </dl>
 
455
      </td>
 
456
    </tr>
 
457
 
 
458
    <tr>
 
459
      <td><code>load</code></td>
 
460
      <td>
 
461
        <p>
 
462
        After model attributes are loaded from a sync layer.
 
463
        </p>
 
464
      </td>
 
465
      <td>
 
466
        <dl>
 
467
          <dt><code>parsed</code></dt>
 
468
          <dd>
 
469
            <p>
 
470
            The parsed version of the sync layer's response to the load request.
 
471
            </p>
 
472
          </dd>
 
473
 
 
474
          <dt><code>response</code></dt>
 
475
          <dd>
 
476
            <p>
 
477
            The sync layer's raw, unparsed response to the load request.
 
478
            </p>
 
479
          </dd>
 
480
        </dl>
 
481
      </td>
 
482
    </tr>
 
483
 
 
484
    <tr>
 
485
      <td><code>save</code></td>
 
486
      <td>
 
487
        <p>
 
488
        After model attributes are saved to a sync layer.
 
489
        </p>
 
490
      </td>
 
491
      <td>
 
492
        <dl>
 
493
          <dt><code>parsed</code></dt>
 
494
          <dd>
 
495
            <p>
 
496
            The parsed version of the sync layer's response to the save request.
 
497
            </p>
 
498
          </dd>
 
499
 
 
500
          <dt><code>response</code></dt>
 
501
          <dd>
 
502
            <p>
 
503
            The sync layer's raw, unparsed response to the save request.
 
504
            </p>
 
505
          </dd>
 
506
        </dl>
 
507
      </td>
 
508
    </tr>
 
509
  </tbody>
 
510
</table>
 
511
 
 
512
<p>
 
513
A model's events bubble up to any <a href="../model-list/index.html">model lists</a> that the model belongs to. This enables you to use the model list as a central point for handling model value changes and errors.
 
514
</p>
 
515
 
 
516
<h4 id="change-events">Change Events</h4>
 
517
 
 
518
<p>
 
519
In addition to the master <code>change</code> event, which is fired whenever one or more attributes are changed, there are also change events for each individual attribute.
 
520
</p>
 
521
 
 
522
<p>
 
523
Attribute-level change events follow the naming scheme <code><em>name</em>Change</code>, where <em>name</em> is the name of an attribute.
 
524
</p>
 
525
 
 
526
<pre class="code prettyprint">&#x2F;&#x2F; Bake a new pie model.
 
527
var pie = new Y.PieModel();
 
528
 
 
529
&#x2F;&#x2F; Listen for all attribute changes.
 
530
pie.on(&#x27;change&#x27;, function (e) {
 
531
  Y.log(&#x27;change fired: &#x27; + Y.Object.keys(e.changed).join(&#x27;, &#x27;));
 
532
});
 
533
 
 
534
&#x2F;&#x2F; Listen for changes to the &#x60;slices&#x60; attribute.
 
535
pie.on(&#x27;slicesChange&#x27;, function (e) {
 
536
  Y.log(&#x27;slicesChange fired&#x27;);
 
537
});
 
538
 
 
539
&#x2F;&#x2F; Listen for changes to the &#x60;type&#x60; attribute.
 
540
pie.on(&#x27;typeChange&#x27;, function (e) {
 
541
  Y.log(&#x27;typeChange fired&#x27;);
 
542
});
 
543
 
 
544
&#x2F;&#x2F; Change multiple attributes at once.
 
545
pie.setAttrs({
 
546
  slices: 3,
 
547
  type  : &#x27;maple custard&#x27;
 
548
});
 
549
 
 
550
&#x2F;&#x2F; =&gt; &quot;slicesChange fired&quot;
 
551
&#x2F;&#x2F; =&gt; &quot;typeChange fired&quot;
 
552
&#x2F;&#x2F; =&gt; &quot;change fired: slices, type&quot;</pre>
 
553
 
 
554
 
 
555
<h4 id="firing-your-own-error-events">Firing Your Own Error Events</h4>
 
556
 
 
557
<p>
 
558
In your custom model class, there may be situations beyond just parsing and validating in which an <code>error</code> event would be useful. For example, in the <code>Y.PieModel</code> class, we fire an error when someone tries to eat a slice of pie and there are no slices left.
 
559
</p>
 
560
 
 
561
<pre class="code prettyprint">&#x2F;&#x2F; Consumes a slice of pie, or fires an &#x60;error&#x60; event if there are no slices
 
562
&#x2F;&#x2F; left.
 
563
eatSlice: function () {
 
564
  if (this.allGone()) {
 
565
    this.fire(&#x27;error&#x27;, {
 
566
      type : &#x27;eat&#x27;,
 
567
      error: &quot;Oh snap! There isn&#x27;t any pie left.&quot;
 
568
    });
 
569
  } else {
 
570
    this.set(&#x27;slices&#x27;, this.get(&#x27;slices&#x27;) - 1);
 
571
    Y.log(&#x27;You just ate a slice of delicious &#x27; + this.get(&#x27;type&#x27;) + &#x27; pie!&#x27;);
 
572
  }
 
573
}</pre>
 
574
 
 
575
 
 
576
<p>
 
577
When firing an error event, set the <code>type</code> property to something that users of your class can use to identify the type of error that has occurred. In the example above, we set it to "eat", because it occurred when the caller tried to eat a slice of pie.
 
578
</p>
 
579
 
 
580
<p>
 
581
The <code>error</code> property should be set to an error message, an Error object, or anything else that provides information about the error and has a <code>toString()</code> method that returns an error message string.
 
582
</p>
 
583
 
 
584
<h3 id="working-with-model-data">Working with Model Data</h3>
 
585
 
 
586
<h4 id="getting-and-setting-attribute-values">Getting and Setting Attribute Values</h4>
 
587
 
 
588
<p>
 
589
Model's <code>get()</code> and <code>set()</code> methods are the main ways of interacting with model attributes. Unsurprisingly, they allow you to get and set the value of a single attribute.
 
590
</p>
 
591
 
 
592
<pre class="code prettyprint">var pie = new Y.PieModel();
 
593
 
 
594
&#x2F;&#x2F; Set the value of the &#x60;type&#x60; attribute.
 
595
pie.set(&#x27;type&#x27;, &#x27;banana cream&#x27;);
 
596
 
 
597
&#x2F;&#x2F; Get the value of the &#x60;type&#x60; attribute.
 
598
pie.get(&#x27;type&#x27;); &#x2F;&#x2F; =&gt; &quot;banana cream&quot;</pre>
 
599
 
 
600
 
 
601
<p>
 
602
Model also provides two convenience methods for getting and escaping an attribute value in a single step. The <code>getAsHTML()</code> method returns an HTML-escaped value, and the <code>getAsURL()</code> method returns a URL-encoded value.
 
603
</p>
 
604
 
 
605
<pre class="code prettyprint">pie.set(&#x27;type&#x27;, &#x27;strawberries &amp; cream&#x27;);
 
606
 
 
607
pie.getAsHTML(&#x27;type&#x27;); &#x2F;&#x2F; =&gt; &quot;strawberries &amp;amp; cream&quot;
 
608
pie.getAsURL(&#x27;type&#x27;);  &#x2F;&#x2F; =&gt; &quot;strawberries%20%26%20cream&quot;</pre>
 
609
 
 
610
 
 
611
<p>
 
612
The <code>getAttrs()</code> and <code>setAttrs()</code> methods may be used to get and set multiple attributes at once. <code>getAttrs()</code> returns a hash of all attribute values, while <code>setAttrs()</code> accepts a hash of attribute values. When you set multiple attributes with <code>setAttrs()</code>, it fires only a single change event that contains all the affected attributes..
 
613
</p>
 
614
 
 
615
<pre class="code prettyprint">pie.setAttrs({
 
616
  slices: 6,
 
617
  type  : &#x27;marionberry&#x27;
 
618
});
 
619
 
 
620
pie.getAttrs();
 
621
&#x2F;&#x2F; =&gt; {
 
622
&#x2F;&#x2F;   clientId: &quot;pieModel_1&quot;,
 
623
&#x2F;&#x2F;   destroyed: false,
 
624
&#x2F;&#x2F;   id: null,
 
625
&#x2F;&#x2F;   initialized: true,
 
626
&#x2F;&#x2F;   slices: 6,
 
627
&#x2F;&#x2F;   type: &quot;marionberry&quot;
 
628
&#x2F;&#x2F; }</pre>
 
629
 
 
630
 
 
631
<p>
 
632
The <code>destroyed</code> and <code>initialized</code> attributes you see in the sample output above are lifecycle attributes provided by <code>Y.Base</code>, and aren't actually model data.
 
633
</p>
 
634
 
 
635
<p>
 
636
To get a slightly more useful representation of model data, use the <code>toJSON()</code> method. The <code>toJSON()</code> method excludes the <code>clientId</code>, <code>destroyed</code>, and <code>initialized</code> attributes, making the resulting object more suitable for serialization and for storing in a persistence layer.
 
637
</p>
 
638
 
 
639
<pre class="code prettyprint">pie.toJSON();
 
640
&#x2F;&#x2F; =&gt; {
 
641
&#x2F;&#x2F;   id: null,
 
642
&#x2F;&#x2F;   slices: 6,
 
643
&#x2F;&#x2F;   type: &quot;marionberry&quot;
 
644
&#x2F;&#x2F; }</pre>
 
645
 
 
646
 
 
647
<p>
 
648
If a custom <code>idAttribute</code> is in use, the <code>toJSON()</code> output will include that id attribute instead of <code>id</code>.
 
649
</p>
 
650
 
 
651
<pre class="code prettyprint">&#x2F;&#x2F; toJSON() output with a custom id attribute.
 
652
pie.toJSON();
 
653
&#x2F;&#x2F; =&gt; {
 
654
&#x2F;&#x2F;   customId: null,
 
655
&#x2F;&#x2F;   slices: 6,
 
656
&#x2F;&#x2F;   type: &quot;marionberry&quot;
 
657
&#x2F;&#x2F; }</pre>
 
658
 
 
659
 
 
660
<p>
 
661
If you'd like to customize the serialized representation of your models, you may override the <code>toJSON()</code> method.
 
662
</p>
 
663
 
 
664
<p>
 
665
When using the <code>set()</code> and <code>setAttrs()</code> methods, you may pass an optional <code>options</code> argument. If <code>options.silent</code> is <code>true</code>, no <code>change</code> event will be fired.
 
666
</p>
 
667
 
 
668
<pre class="code prettyprint">&#x2F;&#x2F; Set attributes without firing a &#x60;change&#x60; event.
 
669
pie.set(&#x27;slices&#x27;, 0, {silent: true});
 
670
 
 
671
pie.setAttrs({
 
672
  slices: 0,
 
673
  type  : &#x27;chocolate cream&#x27;
 
674
}, {silent: true});</pre>
 
675
 
 
676
 
 
677
<p>
 
678
After making changes to a model's attributes, you may call the <code>undo()</code> method to undo the previous change.
 
679
</p>
 
680
 
 
681
<pre class="code prettyprint">var pie = new Y.PieModel({slices: 6});
 
682
 
 
683
pie.set(&#x27;slices&#x27;, 5);
 
684
pie.undo();
 
685
pie.get(&#x27;slices&#x27;); &#x2F;&#x2F; =&gt; 6</pre>
 
686
 
 
687
 
 
688
<p>
 
689
Note that there's only a single level of undo, so it's not possible to revert past the most recent change.
 
690
</p>
 
691
 
 
692
<h4 id="validating-changes">Validating Changes</h4>
 
693
 
 
694
<p>
 
695
Validating model changes as they occur is a good way to ensure that the data in your models isn't nonsense, especially when dealing with user input.
 
696
</p>
 
697
 
 
698
<p>
 
699
There are two ways to validate model attributes. One way is to define an attribute validator function for an individual attribute when you extend <code>Y.Model</code>.
 
700
</p>
 
701
 
 
702
<pre class="code prettyprint">&#x2F;&#x2F; Defining an individual attribute validator.
 
703
Y.PieModel = Y.Base.create(&#x27;pieModel&#x27;, Y.Model, [], {
 
704
  &#x2F;&#x2F; ... prototype methods and properties ...
 
705
}, {
 
706
  ATTRS: {
 
707
    slices: {
 
708
      value: 6,
 
709
      validator: function (value) {
 
710
        return typeof value === &#x27;number&#x27; &amp;&amp; value &gt;= 0 &amp;&amp; value &lt;= 10;
 
711
      }
 
712
    },
 
713
 
 
714
    &#x2F;&#x2F; ...
 
715
  }
 
716
});</pre>
 
717
 
 
718
 
 
719
<p>
 
720
An attribute validator will be called whenever that attribute changes, and can prevent the change from taking effect by returning <code>false</code>. Model <code>error</code> events are not fired when attribute validators fail. For more details on attribute validators, see the <a href="../attribute/index.html">Attribute User Guide</a>.
 
721
</p>
 
722
 
 
723
<p>
 
724
The second way to validate changes is to provide a custom <code>validate()</code> method when extending <code>Y.Model</code>. The <code>validate()</code> method will be called automatically when <code>save()</code> is called, and will receive a hash of all the attributes in the model. If the <code>validate()</code> method returns anything other than <code>null</code> or <code>undefined</code>, it will be considered a validation failure, an <code>error</code> event will be fired with the returned value as the error message, and the <code>save()</code> action will be aborted.
 
725
</p>
 
726
 
 
727
<pre class="code prettyprint">&#x2F;&#x2F; Defining a model-wide &#x60;validator()&#x60; method.
 
728
Y.PieModel = Y.Base.create(&#x27;pieModel&#x27;, Y.Model, [], {
 
729
  &#x2F;&#x2F; ... prototype methods and properties ...
 
730
 
 
731
  validate: function (attributes) {
 
732
    var slices;
 
733
 
 
734
    switch (attributes.type) {
 
735
    case &#x27;rhubarb&#x27;:
 
736
      return &#x27;Eww. No. Not allowed.&#x27;;
 
737
 
 
738
    case &#x27;maple custard&#x27;:
 
739
      slices = Y.Lang.isValue(attributes.slices) ?
 
740
          attributes.slices : this.get(&#x27;slices&#x27;);
 
741
 
 
742
      if (slices &lt; 10) {
 
743
        return &quot;Making fewer than 10 slices of maple custard pie would be &quot; +
 
744
            &quot;silly, because I&#x27;m just going to eat 8 of them as soon as it&#x27;s &quot; +
 
745
            &quot;out of the oven.&quot;;
 
746
      }
 
747
    }
 
748
  }
 
749
}, {
 
750
  &#x2F;&#x2F; ... attributes ...
 
751
});</pre>
 
752
 
 
753
 
 
754
<h4 id="loading-and-saving-model-data">Loading and Saving Model Data</h4>
 
755
 
 
756
<p>
 
757
Calling a model's <code>load()</code> and <code>save()</code> methods will result in a behind-the-scenes call to the model's <code>sync()</code> method specifying the appropriate action.
 
758
</p>
 
759
 
 
760
<p>
 
761
The default <code>sync()</code> method does nothing, but by overriding it and providing your own sync layer, you can make it possible to create, read, update, and delete models from a persistence layer or a server. See <a href="#implementing-a-model-sync-layer">Implementing a Model Sync Layer</a> for more details.
 
762
</p>
 
763
 
 
764
<p>
 
765
The <code>load()</code> and <code>save()</code> methods each accept two optional parameters: an options object and a callback function. If provided, the options object will be passed to the sync layer, and the callback will be called after the load or save operation is finished. You may provide neither parameter, or just an options object, or just a callback, although if you provide both an options object and a callback, they need to be in that order.
 
766
</p>
 
767
 
 
768
<p>
 
769
The callback function will receive two parameters: an error (this parameter will be <code>null</code> or <code>undefined</code> if the operation was successful) and a response (which is the result of calling the model's <code>parse()</code> method with whatever response the sync layer returned).
 
770
</p>
 
771
 
 
772
<p>
 
773
The <code>load()</code> method calls <code>sync()</code> with the <code>read</code> action, and automatically updates the model with the response data (by passing it to <code>parse()</code> and then updating attributes based on the hash that <code>parse()</code> returns) before calling the callback.
 
774
</p>
 
775
 
 
776
<pre class="code prettyprint">var pie = new Y.PieModel({id: &#x27;pie123&#x27;});
 
777
 
 
778
&#x2F;&#x2F; Load a pie model, passing a callback that will run when the model has
 
779
&#x2F;&#x2F; been loaded.
 
780
pie.load(function (err, response) {
 
781
  if (!err) {
 
782
    &#x2F;&#x2F; Success! The model now contains the loaded data.
 
783
  }
 
784
});</pre>
 
785
 
 
786
 
 
787
<p>
 
788
The <code>save()</code> method will do one of two things: if the model is new (meaning it doesn't yet have an <code>id</code> value), it will call <code>sync()</code> with the "create" action. If the model is not new, then it will call <code>sync()</code> with the "update" action.
 
789
</p>
 
790
 
 
791
<p>
 
792
If the sync layer returns a response, then <code>save()</code> will update the model's attributes with the response data before calling the callback function.
 
793
</p>
 
794
 
 
795
<pre class="code prettyprint">&#x2F;&#x2F; Save a pie model, passing a callback that will run when the model has
 
796
&#x2F;&#x2F; been saved.
 
797
pie.save(function (err, response) {
 
798
  if (!err) {
 
799
    &#x2F;&#x2F; Success! The model has been saved. If the sync layer returned a response,
 
800
    &#x2F;&#x2F; then the model has also been updated with any new data in the response.
 
801
  }
 
802
});</pre>
 
803
 
 
804
 
 
805
<p>
 
806
In addition to calling the specified callback (if any), the <code>load()</code> and <code>save()</code> methods will fire a <code>load</code> event and a <code>save</code> event respectively on success, or an <code>error</code> event on failure. See <a href="#model-events">Model Events</a> for more details on these events.
 
807
</p>
 
808
 
 
809
<p>
 
810
Always use the <code>load()</code> or <code>save()</code> methods rather than calling <code>sync()</code> directly, since this ensures that the sync layer's response is passed through the <code>parse()</code> method and that the model's data is updated if necessary.
 
811
</p>
 
812
 
 
813
<h2 id="the-model-lifecycle">The Model Lifecycle</h2>
 
814
 
 
815
<p>
 
816
When a model is first created and doesn't yet have an <code>id</code> value, it's considered "new". The <code>isNew()</code> method will tell you whether or not a model is new.
 
817
</p>
 
818
 
 
819
<p>
 
820
Once a model has an <code>id</code> value, either because one was manually set or because the model received one when it was loaded or saved, the model is no longer considered new, since it is assumed to exist in a persistence layer or on a server somewhere.
 
821
</p>
 
822
 
 
823
<p>
 
824
If a model is new or if any of its attributes have changed since the model was last loaded or saved, the model is considered "modified". The <code>isModified()</code> method will tell you whether or not a model is modified. A successful call to <code>load()</code> or <code>save()</code> will reset a model's "modified" flag.
 
825
</p>
 
826
 
 
827
<p>
 
828
Finally, a model's <code>destroy()</code> method can be used to destroy the model instance. Calling <code>destroy()</code> with no arguments will destroy only the local model instance, while calling <code>destroy({remove: true})</code> will both destroy the local model instance and call the sync layer with the "delete" action to delete the model from a persistence layer or server.
 
829
</p>
 
830
 
 
831
<p>
 
832
It's not necessary for a model to support all possible sync actions. A model that's used to represent read-only data might use a sync layer that only implements the <code>read</code> action, for instance. In this case, the other actions should simply be no-ops that either call the sync callback immediately, or pass an error to the sync callback indicating that the action isn't supported (depending on your personal preference).
 
833
</p>
 
834
 
 
835
<h2 id="implementing-a-model-sync-layer">Implementing a Model Sync Layer</h2>
 
836
 
 
837
<p>
 
838
A model's <code>save()</code>, <code>load()</code>, and <code>destroy()</code> methods all internally call the model's <code>sync()</code> method to carry out an action. The default <code>sync()</code> method doesn't actually do anything, but by overriding the <code>sync()</code> method, you can provide a custom sync layer.
 
839
</p>
 
840
 
 
841
<p>
 
842
A sync layer might make Ajax requests to a remote server, or it might act as a wrapper around local storage, or any number of other things.
 
843
</p>
 
844
 
 
845
<h3 id="the-sync-method">The <code>sync()</code> Method</h3>
 
846
 
 
847
<p>
 
848
When the <code>sync()</code> method is called, it receives three arguments:
 
849
</p>
 
850
 
 
851
<dl>
 
852
  <dt><strong><code>action</code> (<em>String</em>)</strong></dt>
 
853
  <dd>
 
854
    <p>
 
855
    A string that indicates the intended sync action. May be one of the following values:
 
856
    </p>
 
857
 
 
858
    <dl>
 
859
      <dt><strong><code>create</code></strong></dt>
 
860
      <dd>
 
861
        <p>
 
862
        Create a new model record. The "create" action occurs when a model is saved and doesn't yet have an <code>id</code> value.
 
863
        </p>
 
864
      </dd>
 
865
 
 
866
      <dt><strong><code>read</code></strong></dt>
 
867
      <dd>
 
868
        <p>
 
869
        Read an existing model record. The record should be identified based on the <code>id</code> attribute of the model.
 
870
        </p>
 
871
      </dd>
 
872
 
 
873
      <dt><strong><code>update</code></strong></dt>
 
874
      <dd>
 
875
        <p>
 
876
        Update an existing model record. The "update" action occurs when a model is saved and already has an <code>id</code> value. The record to be updated should be identified based on the <code>id</code> attribute of the model.
 
877
        </p>
 
878
      </dd>
 
879
 
 
880
      <dt><strong><code>delete</code></strong></dt>
 
881
      <dd>
 
882
        <p>
 
883
        Delete an existing model record. The record to be deleted should be identified based on the <code>id</code> attribute of the model.
 
884
        </p>
 
885
      </dd>
 
886
    </dl>
 
887
  </dd>
 
888
 
 
889
  <dt><strong><code>options</code> (<em>Object</em>)</strong></dt>
 
890
  <dd>
 
891
    <p>
 
892
    A hash containing any options that were passed to the <code>save()</code>, <code>load()</code> or <code>destroy()</code> method. This may be used to pass custom options to a sync layer.
 
893
    </p>
 
894
  </dd>
 
895
 
 
896
  <dt><strong><code>callback</code> (<em>Function</em>)</strong></dt>
 
897
  <dd>
 
898
    <p>
 
899
    A callback function that should be called when the sync operation is complete. The callback expects to receive the following arguments:
 
900
    </p>
 
901
 
 
902
    <dl>
 
903
      <dt><strong><code>err</code></strong></dt>
 
904
      <dd>
 
905
        <p>
 
906
        Error message or object if an error occured, <code>null</code> or <code>undefined</code> if the operation was successful.
 
907
        </p>
 
908
      </dd>
 
909
 
 
910
      <dt><strong><code>response</code></strong></dt>
 
911
      <dd>
 
912
        <p>
 
913
        Response from the persistence layer, if any. This will be passed to the <code>parse()</code> method to be parsed.
 
914
        </p>
 
915
      </dd>
 
916
    </dl>
 
917
  </dd>
 
918
</dl>
 
919
 
 
920
<p>
 
921
Implementing a sync layer is as simple as handling the requested sync action and then calling the callback function. Here's a sample sync layer that stores records in local storage (note: this example requires the <code>json-stringify</code> module):
 
922
</p>
 
923
 
 
924
<pre class="code prettyprint">Y.PieModel = Y.Base.create(&#x27;pieModel&#x27;, Y.Model, [], {
 
925
  &#x2F;&#x2F; ... prototype methods and properties ...
 
926
 
 
927
  &#x2F;&#x2F; Custom sync layer.
 
928
  sync: function (action, options, callback) {
 
929
    var data;
 
930
 
 
931
    switch (action) {
 
932
      case &#x27;create&#x27;:
 
933
        data = this.toJSON();
 
934
 
 
935
        &#x2F;&#x2F; Use the current timestamp as an id just to simplify the example. In a
 
936
        &#x2F;&#x2F; real sync layer, you&#x27;d want to generate an id that&#x27;s more likely to
 
937
        &#x2F;&#x2F; be globally unique.
 
938
        data.id = Y.Lang.now();
 
939
 
 
940
        &#x2F;&#x2F; Store the new record in localStorage, then call the callback.
 
941
        localStorage.setItem(data.id, Y.JSON.stringify(data));
 
942
        callback(null, data);
 
943
        return;
 
944
 
 
945
      case &#x27;read&#x27;:
 
946
        &#x2F;&#x2F; Look for an item in localStorage with this model&#x27;s id.
 
947
        data = localStorage.getItem(this.get(&#x27;id&#x27;));
 
948
 
 
949
        if (data) {
 
950
          callback(null, data);
 
951
        } else {
 
952
          callback(&#x27;Model not found.&#x27;);
 
953
        }
 
954
 
 
955
        return;
 
956
 
 
957
      case &#x27;update&#x27;:
 
958
        data = this.toJSON();
 
959
 
 
960
        &#x2F;&#x2F; Update the record in localStorage, then call the callback.
 
961
        localStorage.setItem(this.get(&#x27;id&#x27;), Y.JSON.stringify(data));
 
962
        callback(null, data);
 
963
        return;
 
964
 
 
965
      case &#x27;delete&#x27;:
 
966
        localStorage.removeItem(this.get(&#x27;id&#x27;));
 
967
        callback();
 
968
        return;
 
969
 
 
970
      default:
 
971
        callback(&#x27;Invalid action&#x27;);
 
972
    }
 
973
  }
 
974
}, {
 
975
  &#x2F;&#x2F; ... attributes ...
 
976
});</pre>
 
977
 
 
978
 
 
979
<h3 id="the-parse-method">The <code>parse()</code> Method</h3>
 
980
 
 
981
<p>
 
982
Depending on the kind of response your sync layer returns, you may need to override the <code>parse()</code> method as well. The default <code>parse()</code> implementation knows how to parse JavaScript objects and JSON strings that can be parsed into JavaScript objects representing a hash of attributes. If your response data is in another format, such as a nested JSON response or XML, override the <code>parse()</code> method to provide a custom parser implementation.
 
983
</p>
 
984
 
 
985
<p>
 
986
If an error occurs while parsing a response, fire an <code>error</code> event with <code>type</code> "parse".
 
987
</p>
 
988
 
 
989
<p>
 
990
This sample demonstrates a custom parser for responses in which the model data is contained in a <code>data</code> property of the response object.
 
991
</p>
 
992
 
 
993
<pre class="code prettyprint">&#x2F;&#x2F; Custom response parser.
 
994
parse: function (response) {
 
995
  if (response.data) {
 
996
    return response.data;
 
997
  }
 
998
 
 
999
  this.fire(&#x27;error&#x27;, {
 
1000
    type : &#x27;parse&#x27;,
 
1001
    error: &#x27;No data in the response.&#x27;
 
1002
  });
 
1003
}</pre>
 
1004
 
 
1005
</div>
 
1006
            </div>
 
1007
        </div>
 
1008
 
 
1009
        <div class="yui3-u-1-4">
 
1010
            <div class="sidebar">
 
1011
                
 
1012
                    <div id="toc" class="sidebox">
 
1013
                        <div class="hd">
 
1014
                            <h2 class="no-toc">Table of Contents</h2>
 
1015
                        </div>
 
1016
 
 
1017
                        <div class="bd">
 
1018
                            <ul class="toc">
 
1019
<li>
 
1020
<a href="#getting-started">Getting Started</a>
 
1021
</li>
 
1022
<li>
 
1023
<a href="#what-is-a-model">What is a Model?</a>
 
1024
</li>
 
1025
<li>
 
1026
<a href="#using-model">Using Model</a>
 
1027
<ul class="toc">
 
1028
<li>
 
1029
<a href="#quick-start">Quick Start</a>
 
1030
</li>
 
1031
<li>
 
1032
<a href="#extending-ymodel">Extending <code>Y.Model</code></a>
 
1033
</li>
 
1034
<li>
 
1035
<a href="#model-attributes">Model Attributes</a>
 
1036
<ul class="toc">
 
1037
<li>
 
1038
<a href="#built-in-attributes">Built-in Attributes</a>
 
1039
</li>
 
1040
<li>
 
1041
<a href="#custom-attributes">Custom Attributes</a>
 
1042
</li>
 
1043
</ul>
 
1044
</li>
 
1045
<li>
 
1046
<a href="#model-properties">Model Properties</a>
 
1047
</li>
 
1048
<li>
 
1049
<a href="#model-events">Model Events</a>
 
1050
<ul class="toc">
 
1051
<li>
 
1052
<a href="#change-events">Change Events</a>
 
1053
</li>
 
1054
<li>
 
1055
<a href="#firing-your-own-error-events">Firing Your Own Error Events</a>
 
1056
</li>
 
1057
</ul>
 
1058
</li>
 
1059
<li>
 
1060
<a href="#working-with-model-data">Working with Model Data</a>
 
1061
<ul class="toc">
 
1062
<li>
 
1063
<a href="#getting-and-setting-attribute-values">Getting and Setting Attribute Values</a>
 
1064
</li>
 
1065
<li>
 
1066
<a href="#validating-changes">Validating Changes</a>
 
1067
</li>
 
1068
<li>
 
1069
<a href="#loading-and-saving-model-data">Loading and Saving Model Data</a>
 
1070
</li>
 
1071
</ul>
 
1072
</li>
 
1073
</ul>
 
1074
</li>
 
1075
<li>
 
1076
<a href="#the-model-lifecycle">The Model Lifecycle</a>
 
1077
</li>
 
1078
<li>
 
1079
<a href="#implementing-a-model-sync-layer">Implementing a Model Sync Layer</a>
 
1080
<ul class="toc">
 
1081
<li>
 
1082
<a href="#the-sync-method">The <code>sync()</code> Method</a>
 
1083
</li>
 
1084
<li>
 
1085
<a href="#the-parse-method">The <code>parse()</code> Method</a>
 
1086
</li>
 
1087
</ul>
 
1088
</li>
 
1089
</ul>
 
1090
                        </div>
 
1091
                    </div>
 
1092
                
 
1093
 
 
1094
                
 
1095
 
 
1096
                
 
1097
                    <div class="sidebox">
 
1098
                        <div class="hd">
 
1099
                            <h2 class="no-toc">Examples That Use This Component</h2>
 
1100
                        </div>
 
1101
 
 
1102
                        <div class="bd">
 
1103
                            <ul class="examples">
 
1104
                                
 
1105
                                    
 
1106
                                        <li data-description="A basic todo list built with the Model, Model List, and View components.">
 
1107
                                            <a href="../app/app-todo.html">Todo List</a>
 
1108
                                        </li>
 
1109
                                    
 
1110
                                
 
1111
                                    
 
1112
                                        <li data-description="An application to browse through the contributors of a GitHub project.">
 
1113
                                            <a href="../app/app-contributors.html">GitHub Contributors</a>
 
1114
                                        </li>
 
1115
                                    
 
1116
                                
 
1117
                            </ul>
 
1118
                        </div>
 
1119
                    </div>
 
1120
                
 
1121
            </div>
 
1122
        </div>
 
1123
    </div>
 
1124
</div>
 
1125
 
 
1126
<script src="../assets/vendor/prettify/prettify-min.js"></script>
 
1127
<script>prettyPrint();</script>
 
1128
 
 
1129
</body>
 
1130
</html>