/
getting_started_knockback_important_details.html
229 lines (220 loc) · 13.1 KB
/
getting_started_knockback_important_details.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Getting Started: Knockback.js Important Details</title>
<link rel="stylesheet" href="./vendor/css/normalize.css">
<link rel="stylesheet" href="./vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="./vendor/bootstrap/css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="./vendor/google-code-prettify/prettify.css">
<link rel="stylesheet" href="./css/layout.css">
<link rel="stylesheet" href="./css/layout_components.css">
<link rel="icon" type="image/x-icon" href="./img/favicon.ico">
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="css/tutorial.css">
<script type="application/javascript" src="vendor/globalize/globalize.js"></script>
<script type="application/javascript" src="vendor/globalize/globalize.culture.en-GB.js"></script>
<script type="application/javascript" src="vendor/globalize/globalize.culture.fr-FR.js"></script>
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script type="application/javascript" src="vendor/js/knockback-1.2.3.js"></script>
<script type="application/javascript" src="./vendor/bootstrap/js/bootstrap.min.js"></script>
<script type="application/javascript" src="./vendor/google-code-prettify/prettify.js"></script>
<script type="application/javascript" src="./js/stylecode.js"></script>
<script type="application/javascript" src="./js/interface.js"></script>
</head>
<body>
<div class="page navbar">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container"><a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></a><a class="brand kb" href="https://github.com/kmalakoff/knockback">Knockback.js</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li><a href="doc/index.html">API</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Getting Started<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
<li><a href="getting_started_introduction.html">Overview</a><a href="getting_started_dependent_library_basics.html">Dependent Library Basics</a><a href="getting_started_knockback_basics.html">Knockback Basics</a><a href="getting_started_views_view_models_models.html">Models, Views, and ViewModels</a><a href="getting_started_memory_lifecycle_management.html">Memory and Lifecycle Management</a><a href="getting_started_knockback_important_details.html">Knockback Important Details</a>
</li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Components<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
<li><a href="components_backbone_relational.html">Backbone Relational</a><a href="components_backbone_modelref.html">Backbone ModelRef</a>
</li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Applications<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
<li><a href="app_todos.html">Todos App</a>
</li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Tutorials<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
<li><a href="tutorials_introduction.html">Introduction</a><a href="tutorial_kb_observable.html">kb.Observable</a><a href="tutorial_kb_view_model.html">kb.ViewModel</a><a href="tutorial_kb_collection_observable.html">kb.CollectionObservable</a><a href="tutorial_nested_models.html">Nested Models</a><a href="tutorial_relational_models.html">Relational Models</a><a href="tutorial_kb_localized_observable.html">kb.LocalizedObservable</a><a href="tutorial_locale_manager.html">Custom Locale Manager</a><a href="tutorial_inject.html">ViewModel Injection</a>
</li>
</ul>
</li>
<li><a href="https://github.com/kmalakoff/knockback/blob/master/RELEASE_NOTES.md">Release Notes</a></li>
</ul>
<div id="script_language">
<div class="separator">
<div class="selection codestyle">
<div class="option">
<input class="auto" id="cs-cs" type="radio" name="codestyle" value="cs"/>
<label for="cs-cs">CS</label>
</div>
<div class="option">
<input id="cs-js" type="radio" name="codestyle" value="js"/>
<label for="cs-js">JS</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<header class="page">
</header>
<content class="page">
<section>
<h1>Getting Started with Knockback.js: Knockback.js Important Details</h1>
<p>#Inheritance
Some Knockback classes are meant to be inherited from. Specifically:</p>
<ul>
<li><a href="doc/class/kb/ViewModel.html">kb.ViewModel</a></li>
<li><a href="doc/class/kb/CollectionObservable.html">kb.CollectionObservable</a></li>
<li><a href="doc/class/kb/LocalizedObservable.html">kb.LocalizedObservable</a></li>
</ul>
<p>When using CoffeeScript, you can derive from a super class using:</p>
<pre><code>class YourClass extends SuperClass
# extensions
</code></pre>
<p>or when using JavaScript, you can derive from a super class using:</p>
<pre><code>var YourClass = SuperClass.extend({
/* extensions */
})
</code></pre>
<p>For example:</p>
<div class="row-fluid">
<div class="span8">
<ul class="nav nav-tabs">
<li class="active"><a href="#kb_view_model_inheritance_view" data-toggle="tab">View (HTML)</a></li>
<li><a href="#kb_view_model_inheritance_view_model" data-toggle="tab">ViewModel (CS/JS)</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="kb_view_model_inheritance_view">
<pre><div id='kb_view_model_inheritance'>
<p>First name: <input class='input-small pull-right' data-bind="value: first_name, valueUpdate: 'keyup'" /></p>
<p>Last name: <input class='input-small pull-right' data-bind="value: last_name, valueUpdate: 'keyup'" /></p>
<p>Hello, <span data-bind="text: full_name"> </span>!</p>
</div></pre>
</div>
<div class="tab-pane" id="kb_view_model_inheritance_view_model">
<pre data-for="cs">model = new Backbone.Model({first_name: "Planet", last_name: "Earth"})
class MyViewModel extends kb.ViewModel
constructor: (model) ->
super
@full_name = ko.computed((->return "#{@first_name()} #{@last_name()}"), @)
view_model = new MyViewModel(model)
ko.applyBindings(view_model, $('#kb_view_model_inheritance')[0])</pre>
<pre data-for="js">var ko = kb.ko;
var model = new Backbone.Model({first_name: "Planet", last_name: "Earth"});
var MyViewModel = kb.ViewModel.extend({
constructor: function(model) {
kb.ViewModel.prototype.constructor.apply(this, arguments);
this.full_name = ko.computed((function() {
return "" + (this.first_name()) + " " + (this.last_name());
}), this);
}
});
var view_model = new MyViewModel(model);
ko.applyBindings(view_model, $('#kb_view_model_inheritance')[0]);
</pre>
</div>
</div>
</div>
<div class="span4">
<h4>Live Result</h4>
<div class="demos_result_box"><div id='kb_view_model_inheritance'>
<p>First name: <input class='input-small pull-right' data-bind="value: first_name, valueUpdate: 'keyup'" /></p>
<p>Last name: <input class='input-small pull-right' data-bind="value: last_name, valueUpdate: 'keyup'" /></p>
<p>Hello, <span data-bind="text: full_name"> </span>!</p>
</div>
<script type="application/javascript" src="getting_started/knockback_important_details/kb_view_model_inheritance.js"></script>
</div>
</div>
</div><p>#Specifying Mappings Paths for Nested Models</p>
<p>Because Backbone.Models can have attributes than contain nested Backbone.Models or Backbone.Collections, there are times where you need to specialize the ViewModel you want to use; for example, in a model/collection hierachy with relationships. When you do need to, you can use the <code>factories</code> option when creating a <a href="doc/class/kb/ViewModel.html">kb.ViewModel</a> or <a href="doc/class/kb/CollectionObservable.html">kb.CollectionObservable</a></p>
<pre data-for="cs">view_model = {
people: kb.collectionObservable(new Backbone.Collection([bob, fred]), {
factories:
'models': PersonViewModel
'models.friends.models': FriendViewModel
})
}</pre>
<pre data-for="js">var view_model = {
people: kb.collectionObservable(new Backbone.Collection([bob, fred]), {
factories: {
'models': PersonViewModel,
'models.friends.models': FriendViewModel
}
})
};</pre><p>Please take a look at the <a href="tutorial_nested_models.html">Nested Model Tutorial</a> for more details.</p>
<p>#Knockout Observables Foundations</p>
<p>Many Knockback.js classes return Knockout.js observables so their dependencies/subscriptions can and should be managed like any Knockout observable. This is **very important if you choose to extend Knockback classes.</p>
<p>These classes return a ko.observable instead of 'this':</p>
<ul>
<li><a href="doc/class/kb/CollectionObservable.html">kb.CollectionObservable</a></li>
<li><a href="doc/class/kb/Observable.html">kb.Observable</a></li>
<li><a href="doc/class/kb/DefaultWrapper.html">kb.DefaultWrapper</a></li>
<li><a href="doc/class/kb/FormattedObservable.html">kb.FormattedObservable</a></li>
<li><a href="doc/class/kb/LocalizedObservable.html">kb.LocalizedObservable</a></li>
<li><a href="doc/class/kb/TriggeredObservable.html">kb.TriggeredObservable</a></li>
</ul>
<p>Use ko.utils.wrappedObservable(this) or return the constructor result if you dervive from them:</p>
<pre><code>YourCollectionObservable = kb.CollectionObservable.extend({
constructor: function(collection, options) {
return kb.CollectionObservable.prototype.constructor.call(this, collection, { view_model: YourViewModel, options: options });
}
});
</code></pre>
<p>or</p>
<pre><code>YourCollectionObservable = kb.CollectionObservable.extend({
constructor: function(collection, options) {
kb.CollectionObservable.prototype.constructor.call(this, collection, { view_model: YourViewModel, options: options });
return ko.utils.wrappedObservable(this);
}
});
</code></pre>
<p>These classes return 'this':</p>
<ul>
<li><a href="doc/class/kb/Factory.html">kb.Factory</a></li>
<li><a href="doc/class/kb/ModelWatcher.html">kb.ModelWatcher</a></li>
<li><a href="doc/class/kb/Store.html">kb.Store</a></li>
<li><a href="doc/class/kb/ViewModel.html">kb.ViewModel</a></li>
<li><a href="doc/class/kb/Statistics.html">kb.Statistics</a></li>
</ul>
<p>#Backbone.Model Signatures</p>
<p>Knockback.js does not require Backbone.Models or Backbone.ModelRefs for attribute watching, but requires only Backbone.Model-like signature.</p>
<p>The minimal Backbone.Model signature is:</p>
<ul>
<li><strong>Backbone.Events</strong>: 'bind(event, callback)', 'unbind(event, callback)', and 'trigger(event, params...)'</li>
<li><strong>Backbone.Model</strong>: 'get(attribute_name)'</li>
<li><strong>Triggering</strong>: 'change' and/or "change:attribute_name" events when the Backbone.Models-like instance's attributes change.</li>
</ul>
<p>This fact is used when implementing a <a href="tutorial_locale_manager.html">Custom Locale Manager</a> and could be used to port another library like Spine.js to Knockback.js.</p>
</section>
</content>
<footer class="page">
</footer>
</div>
</body>
</html>