forked from platanus/angular-restmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial-model_.html
More file actions
238 lines (178 loc) · 7.96 KB
/
Copy pathtutorial-model_.html
File metadata and controls
238 lines (178 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>$restmod tutorialUsing Generated Models</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top ">
<div class="navbar-inner">
<a class="brand" href="index.html">$restmod</a>
<ul class="nav">
<li class="dropdown">
<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="$restmod.html">$restmod</a>
</li>
<li>
<a href="$restmodProvider.html">$restmodProvider</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="BuilderApi.html">BuilderApi</a>
</li>
<li>
<a href="CommonApi.html">CommonApi</a>
</li>
<li>
<a href="RecordApi.html">RecordApi</a>
</li>
<li>
<a href="ScopeApi.html">ScopeApi</a>
</li>
<li>
<a href="StaticApi.html">StaticApi</a>
</li>
<li>
<a href="Utils.html">Utils</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="mixins.list.html" class="dropdown-toggle" data-toggle="dropdown">Mixins<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="DebouncedModel.html">DebouncedModel</a>
</li>
<li>
<a href="DirtyModel.html">DirtyModel</a>
</li>
<li>
<a href="PagedModel.html">PagedModel</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="tutorials.list.html" class="dropdown-toggle" data-toggle="dropdown">Tutorials<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="tutorial-building.html">Model Building</a>
</li>
<li>
<a href="tutorial-model_.html">Using Generated Models</a>
</li>
<li>
<a href="tutorial-relations.html">Using Relations</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<section>
<header>
<h2>Using Generated Models</h2>
</header>
<article>
<p>$restmod provides two levels of model interaction, through collections and instances.</p>
<p>The model type is considered the base collection and is limited to certain collection methods.</p>
<p>This guide will use the following model definition in examples.</p>
<pre><code class="lang-javascript">Bike = $restmod.model('/bikes');</code></pre>
<h2>General Considerations</h2>
<h3>Asyncronic operations</h3>
<p>All asynchronic server operations return inmeditelly:</p>
<ul>
<li>If the operation is expected to return an object (as in ModelCollection#$create or ModelCollection#$find) it will return an unresolved object that will be filled with data when the server responds.</li>
<li>If the operation is expected to update the object data (as in Model#$fetch) it will update the object properties when the server responds.</li>
<li>If the operations returns a collection (as in ModelCollection#$search) it will return an empty collection that will be filled when the server responds.</li>
</ul>
<p>There are several properties that can be queried to track an object request status:</p>
<ul>
<li>It is posible to query the resolve status of an object using the <code>$pending</code> property, it is set to true when a request starts and back to false when it ends.</li>
<li>Its also posible to access the last request response object through the <code>$response</code> property.</li>
<li>If the last response was an error then the <code>$error</code> property will be true.</li>
</ul>
<p>Special care shoult be taken when using these properties since they apply only to the last request. To track more than one request is better to use promises.</p>
<h3>Promises</h3>
<p>The <code>$promise</code> property of an object (model instance or collection) contains the promise made by that object's last request, it can be accessed directly or using the shorthand methods <a href="Model.html#$then">Model#$then</a> and <a href="Model.html#$finally">Model#$finally</a>. The aforementioned methods both change the last promise property and return the object instance so they can be chained.</p>
<pre><code class="lang-javascript">Bike.$find(1).$then(function(_bike) {
alert('success');
}).$finally(function(_bike) {
alert('always');
});</code></pre>
<h2>CRUD Operations</h2>
<p>Building a new model instance is done using the ModelCollection#$build method, the build method will not generate any request.</p>
<pre><code class="lang-javascript">var bike = Bike.$build({ brand: 'Giant', model: 'Anthem' });</code></pre>
<p><strong>IMPORTANT</strong> do not use the default type constructor directly unless you have read the <a href="Model.html">Model</a> documentation.</p>
<p>Creating a model instance can be done using the ModelCollection#$create method, it is called using the collection's base url. If using the type directly then the model's base url is used.</p>
<pre><code class="lang-javascript">var bike = Bike.$create({ brand: 'Giant', model: 'Anthem' }); // will send a POST to /bikes</code></pre>
<p>It is also posible to save an already built object:</p>
<pre><code class="lang-javascript">var bike = Bike.$build({ brand: 'Giant', model: 'Anthem' });
bike.$save(); // will send POST to /bikes</code></pre>
<p>Retrieving an objects is done using the {@link ModelCollection#$find} method.</p>
<pre><code class="lang-javascript">var bike = Bike.$find(22); // will send a GET to /bikes</code></pre>
<p>To update an object just modify the required properties and call ModelCollection#$save, if object is bound (has an id) it will be updated, if not it will be created.</p>
<pre><code class="lang-javascript">var bike = Bike.$find(22); // will send GET to /bikes
console.log(bike.id); // should print out 22
bike.brand = 'Trek';
bike.$save(); // will send a PUT to /bikes/22</code></pre>
<h2>Collections</h2>
<p>TODO</p>
<h2>Relations</h2>
<p>TODO</p>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Wed May 28 2014 18:42:23 GMT-0400 (CLT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
<br clear="both">
</div>
</div>
<script src="scripts/sunlight.js"></script>
<script src="scripts/sunlight.javascript.js"></script>
<script src="scripts/sunlight-plugin.doclinks.js"></script>
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
<script src="scripts/sunlight-plugin.menu.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/jquery.scrollTo.js"></script>
<script src="scripts/jquery.localScroll.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
<script>
$( function () {
$( "#toc" ).toc( {
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
</script>
</body>
</html>