@maccesar/titools 3.2.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -61
- package/lib/cleanup.js +29 -0
- package/lib/commands/agents.js +8 -7
- package/lib/commands/skills.js +118 -23
- package/lib/config.js +13 -15
- package/lib/installer.js +5 -3
- package/lib/utils.js +36 -0
- package/package.json +1 -1
- package/skills/alloy-guides/SKILL.md +188 -0
- package/skills/alloy-guides/references/CLI_TASKS.md +233 -0
- package/skills/alloy-guides/references/CONCEPTS.md +171 -0
- package/skills/alloy-guides/references/CONTROLLERS.md +279 -0
- package/skills/alloy-guides/references/MODELS.md +742 -0
- package/skills/alloy-guides/references/MODELS_ADVANCED.md +482 -0
- package/skills/alloy-guides/references/PURGETSS.md +57 -0
- package/skills/alloy-guides/references/VIEWS_DYNAMIC.md +235 -0
- package/skills/alloy-guides/references/VIEWS_STYLES.md +375 -0
- package/skills/alloy-guides/references/VIEWS_WITHOUT_CONTROLLERS.md +102 -0
- package/skills/alloy-guides/references/VIEWS_XML.md +581 -0
- package/skills/alloy-guides/references/WIDGETS.md +160 -0
- package/skills/alloy-howtos/SKILL.md +179 -0
- package/skills/alloy-howtos/references/best_practices.md +121 -0
- package/skills/alloy-howtos/references/cli_reference.md +230 -0
- package/skills/alloy-howtos/references/config_files.md +158 -0
- package/skills/alloy-howtos/references/custom_tags.md +148 -0
- package/skills/alloy-howtos/references/debugging_troubleshooting.md +78 -0
- package/skills/alloy-howtos/references/samples.md +156 -0
- package/skills/purgetss/SKILL.md +184 -181
- package/skills/purgetss/references/EXAMPLES.md +8 -6
- package/skills/purgetss/references/app-branding.md +5 -0
- package/skills/purgetss/references/apply-directive.md +1 -1
- package/skills/purgetss/references/class-categories.md +1 -1
- package/skills/purgetss/references/class-index-properties.md +618 -0
- package/skills/purgetss/references/class-index.md +4 -610
- package/skills/purgetss/references/cli-commands.md +41 -410
- package/skills/purgetss/references/custom-fonts.md +292 -0
- package/skills/purgetss/references/custom-rules.md +1 -1
- package/skills/purgetss/references/customization-deep-dive.md +64 -1
- package/skills/purgetss/references/icon-fonts.md +208 -118
- package/skills/purgetss/references/installation-setup.md +4 -4
- package/skills/purgetss/references/migration-guide.md +53 -1
- package/skills/purgetss/references/multi-density-images.md +53 -2
- package/skills/purgetss/references/svg-pipeline.md +249 -0
- package/skills/purgetss/references/tikit-components.md +2 -2
- package/skills/purgetss/references/titanium-resets.md +2 -2
- package/skills/purgetss/references/ui-ux-design.md +7 -5
- package/skills/purgetss/references/version-history.md +71 -82
- package/skills/ti-api/SKILL.md +128 -0
- package/skills/ti-api/references/api-android.md +675 -0
- package/skills/ti-api/references/api-app-platform.md +636 -0
- package/skills/ti-api/references/api-core.md +764 -0
- package/skills/ti-api/references/api-data-network.md +641 -0
- package/skills/ti-api/references/api-media.md +655 -0
- package/skills/ti-api/references/api-modules-ble-bluetooth.md +657 -0
- package/skills/ti-api/references/api-modules-coremotion-urlsession.md +411 -0
- package/skills/ti-api/references/api-modules-map.md +632 -0
- package/skills/ti-api/references/api-modules-nfc.md +725 -0
- package/skills/ti-api/references/api-modules-social-misc.md +526 -0
- package/skills/ti-api/references/api-services.md +700 -0
- package/skills/ti-api/references/api-ui-android.md +499 -0
- package/skills/ti-api/references/api-ui-extras.md +702 -0
- package/skills/ti-api/references/api-ui-ios-animator.md +378 -0
- package/skills/ti-api/references/api-ui-ios.md +756 -0
- package/skills/ti-api/references/api-ui-lists.md +581 -0
- package/skills/ti-api/references/api-ui-text-input.md +607 -0
- package/skills/ti-api/references/api-ui-views.md +572 -0
- package/skills/ti-api/references/api-ui-windows-navigation.md +676 -0
- package/skills/ti-api/references/api-xml-global.md +743 -0
- package/skills/ti-guides/SKILL.md +82 -0
- package/skills/ti-guides/references/advanced-data-and-images.md +155 -0
- package/skills/ti-guides/references/android-manifest.md +97 -0
- package/skills/ti-guides/references/app-distribution.md +373 -0
- package/skills/ti-guides/references/application-frameworks.md +366 -0
- package/skills/ti-guides/references/cli-reference.md +700 -0
- package/skills/ti-guides/references/coding-best-practices.md +150 -0
- package/skills/ti-guides/references/commonjs-advanced.md +279 -0
- package/skills/ti-guides/references/hello-world.md +99 -0
- package/skills/ti-guides/references/hyperloop-native-access.md +458 -0
- package/skills/ti-guides/references/javascript-primer.md +402 -0
- package/skills/ti-guides/references/reserved-words.md +36 -0
- package/skills/ti-guides/references/resources.md +172 -0
- package/skills/ti-guides/references/style-and-conventions.md +104 -0
- package/skills/ti-guides/references/tiapp-config.md +655 -0
- package/skills/ti-howtos/SKILL.md +131 -0
- package/skills/ti-howtos/references/android-platform-deep-dives.md +609 -0
- package/skills/ti-howtos/references/automation-fastlane-appium.md +96 -0
- package/skills/ti-howtos/references/buffer-codec-streams.md +162 -0
- package/skills/ti-howtos/references/cross-platform-development.md +358 -0
- package/skills/ti-howtos/references/debugging-profiling.md +473 -0
- package/skills/ti-howtos/references/extending-titanium.md +684 -0
- package/skills/ti-howtos/references/google-maps-v2.md +172 -0
- package/skills/ti-howtos/references/ios-map-kit.md +149 -0
- package/skills/ti-howtos/references/ios-platform-deep-dives.md +595 -0
- package/skills/ti-howtos/references/local-data-sources.md +310 -0
- package/skills/ti-howtos/references/location-and-maps.md +267 -0
- package/skills/ti-howtos/references/media-apis.md +268 -0
- package/skills/ti-howtos/references/notification-services.md +539 -0
- package/skills/ti-howtos/references/remote-data-sources.md +339 -0
- package/skills/ti-howtos/references/tutorials.md +552 -0
- package/skills/ti-howtos/references/using-modules.md +182 -0
- package/skills/ti-howtos/references/web-content-integration.md +288 -0
- package/skills/ti-howtos/references/webpack-build-pipeline.md +125 -0
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
# Alloy Models
|
|
2
|
+
|
|
3
|
+
Models, collections, and how to bind them to views in Alloy. For sync adapters, migrations, plain Backbone usage, and the Backbone 0.9.2 → 1.1.2 / 1.3.3 migration, see [MODELS_ADVANCED.md](MODELS_ADVANCED.md).
|
|
4
|
+
|
|
5
|
+
1. [Overview](#overview)
|
|
6
|
+
2. [Alloy Collection and Model Objects](#alloy-collection-and-model-objects)
|
|
7
|
+
3. [Alloy Data Binding](#alloy-data-binding)
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
Alloy uses Backbone.js to provide support for its models and collections. Alloy also borrows the concepts of migrations and adapters from Rails for storage integration.
|
|
12
|
+
|
|
13
|
+
For models, collections and sync adapters, these guides only provides information on how Alloy uses the Backbone.js functionality and some simple examples of using it.
|
|
14
|
+
|
|
15
|
+
## Alloy Collection and Model Objects
|
|
16
|
+
|
|
17
|
+
### Models
|
|
18
|
+
|
|
19
|
+
In Alloy, models inherit from the [Backbone.Model](https://backbonejs.org/#Model-View-separation) class. They contain the interactive data and logic used to control and access it. Models are specified with JavaScript files, which provide a table schema, adapter configuration and logic to extend the Backbone.Model class. Models are automatically defined and available in the controller scope as the name of the JavaScript file.
|
|
20
|
+
|
|
21
|
+
The JavaScript file exports a definition object comprised of three different objects. The first object, called `config`, defines the table schema and adapter information. The next two objects `extendModel` and `extendCollection` define functions to extend, override or implement the Backbone.Model and Backbone.Collection classes, respectively.
|
|
22
|
+
|
|
23
|
+
**Example of the anatomy of a model file**
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
exports.definition = {
|
|
27
|
+
config : { // table schema and adapter information
|
|
28
|
+
},
|
|
29
|
+
extendModel(Model) {
|
|
30
|
+
_.extend(Model.prototype, { // Extend, override or implement Backbone.Model
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return Model;
|
|
34
|
+
},
|
|
35
|
+
extendCollection(Collection) {
|
|
36
|
+
_.extend(Collection.prototype, { // Extend, override or implement Backbone.Collection
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return Collection;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To access a model locally in a controller, use the `Alloy.createModel` method. The first required parameter is the name of the JavaScript file minus the '.js' extension. The second optional parameter is the attributes for initializing the model object. For example:
|
|
45
|
+
|
|
46
|
+
**Basic model usage**
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
const book = Alloy.createModel('book', {title:'Green Eggs and Ham', author:'Dr. Seuss'});
|
|
50
|
+
const title = book.get('title');
|
|
51
|
+
const author = book.get('author');
|
|
52
|
+
|
|
53
|
+
// Label object in the view with id = 'label'
|
|
54
|
+
$.label.text = title + ' by ' + author;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The `book` model object is a Backbone object wrapped by Alloy, so it can be treated as a Backbone.Model object. You can use any Backbone Model or Events APIs with this object.
|
|
58
|
+
|
|
59
|
+
You can also create a global singleton instance of a model, either in markup or in the controller, which may be accessed in all controllers. Use the `Alloy.Models.instance` method with the name of the model file minus the extension as the only parameter to create or access the singleton. For example:
|
|
60
|
+
|
|
61
|
+
**Working with globally registered models**
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
// This will create a singleton if it has not been previously created,
|
|
65
|
+
// or retrieves the singleton if it already exists.
|
|
66
|
+
const book = Alloy.Models.instance('book');
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### Configuration Object
|
|
70
|
+
|
|
71
|
+
The `config` object is comprised of three different objects: `columns`, `defaults` and `adapter`.
|
|
72
|
+
|
|
73
|
+
The `columns` object defines the table schema information. The key is the record name and the value is the data type. The following data types are accepted and mapped to the appropriate SQLite type: `string`, `varchar`, `int`, `tinyint`, `smallint`, `bigint`, `double`, `float`, `decimal`, `number`, `date`, `datetime` and `boolean`. By default, any unknown data type maps to the SQLite type `TEXT`. Alternatively, the SQLite sync adapter accepts the SQLite keywords.
|
|
74
|
+
|
|
75
|
+
The optional `defaults` object defines the default values for a record if one or more record fields are left undefined upon creation. The key is the record name and the value is the default value.
|
|
76
|
+
|
|
77
|
+
The adapter object defines how to access persistent storage. It contains two key-value pairs: `type` and `collection_name`. The `type` key identifies the sync adapter and the `collection_name` key identifies the name of the table in the database or a namespace.
|
|
78
|
+
|
|
79
|
+
For example, suppose there is a model object called book (`book.js`) defined as:
|
|
80
|
+
|
|
81
|
+
**book.js**
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
exports.definition = {
|
|
85
|
+
config: {
|
|
86
|
+
"columns": {
|
|
87
|
+
"title": "String",
|
|
88
|
+
"author": "String"
|
|
89
|
+
},
|
|
90
|
+
"defaults": {
|
|
91
|
+
"title": "-",
|
|
92
|
+
"author": "-"
|
|
93
|
+
},
|
|
94
|
+
"adapter": {
|
|
95
|
+
"type": "sql",
|
|
96
|
+
"collection_name": "books"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The code above describes a book object, which has two `string` (or `TEXT`) fields: `title` and `author`. If either field is left undefined, it will be assigned with the default value, a dash ("-"). The `sql` type configures Backbone to use the SQL adapter to sync with the SQLite engine on Android and iOS devices to access a table in the database called "books".
|
|
103
|
+
|
|
104
|
+
You may add custom properties to the `config` object, which are available to the application as the model or collection's `config` property or can be processed by a custom sync adapter during application initialization.
|
|
105
|
+
|
|
106
|
+
#### Extending the Backbone.Model Class
|
|
107
|
+
|
|
108
|
+
The Backbone.Model class can be extended using the `extendModel` object, which implements the Backbone.Model `extend` method. This allows the Backbone.js code to be extended, overridden or implemented.
|
|
109
|
+
|
|
110
|
+
For example, the `validate` method is left unimplemented by Backbone.js. The model JS file can implement `validate(attrs)`, where the parameter `attrs` are changed attributes in the model. In Backbone.js, if `validate` is implemented, it is called by the `set` and `save(attributes)` methods before changing the attributes and is also called by the `isValid` method. For the `save` method, validate is called if the `attributes` parameter is defined.
|
|
111
|
+
|
|
112
|
+
In the example code `book.js` below, the JavaScript file implements the validate method, and adds a custom property and function.
|
|
113
|
+
|
|
114
|
+
**Extending a model**
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
exports.definition = {
|
|
118
|
+
config : { // table schema and adapter information
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
extendModel(Model) {
|
|
122
|
+
_.extend(Model.prototype, {
|
|
123
|
+
// Implement the validate method
|
|
124
|
+
validate(attrs) {
|
|
125
|
+
for (const key in attrs) {
|
|
126
|
+
const value = attrs[key];
|
|
127
|
+
if (key === "title") {
|
|
128
|
+
if (value.length <= 0) {
|
|
129
|
+
return "Error: No title!";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (key === "author") {
|
|
133
|
+
if (value.length <= 0) {
|
|
134
|
+
return "Error: No author!";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
// Extend Backbone.Model
|
|
140
|
+
customProperty: 'book',
|
|
141
|
+
customFunction() {
|
|
142
|
+
Ti.API.info('I am a book model.');
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
return Model;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
In the controller, to access the model, do:
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
const book = Alloy.createModel('book', {title:'Green Eggs and Ham', author:'Dr. Seuss'});
|
|
155
|
+
// Since set or save(attribute) is not being called, we can call isValid to validate the model object
|
|
156
|
+
if (book.isValid() && book.customProperty == "book") { // Save data to persistent storage
|
|
157
|
+
book.save();
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
book.destroy();
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Collections
|
|
165
|
+
|
|
166
|
+
Collections are ordered sets of models and inherit from the Backbone.Collection class. Alloy Collections are automatically defined and available in the controller scope as the name of the model. To access a collection in the controller locally, use the `Alloy.createCollection` method with the name of the JavaScript file minus the '.js' extension as the required parameter. The second optional parameter can be an array of model objects for initialization. For example, the code below creates a collection using the previously defined model and reads data from persistent storage:
|
|
167
|
+
|
|
168
|
+
**Creating collections**
|
|
169
|
+
|
|
170
|
+
```javascript
|
|
171
|
+
const library = Alloy.createCollection('book');
|
|
172
|
+
library.fetch(); // Grab data from persistent storage
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The `library` collection object is a Backbone object wrapped by Alloy, so it can be treated as a Backbone.Collection object. You can use any Backbone Collection or Events APIs with this object.
|
|
176
|
+
|
|
177
|
+
You can also create a global singleton instance, either in markup or in the controller, which may be accessed in all controllers. Use the `Alloy.Collections.instance` method with the name of the model file minus the extension as the only parameter to create or access the singleton. For example:
|
|
178
|
+
|
|
179
|
+
**Working with globally registered collections**
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
// This will create a singleton if it has not been previously created,
|
|
183
|
+
// or retrieves the singleton if it already exists.
|
|
184
|
+
const library = Alloy.Collections.instance('book');
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Extending the Backbone.Collection Class
|
|
188
|
+
|
|
189
|
+
Like the Backbone.Model class, the Backbone.Collection class can be similarly extended in the model JavaScript file. For example, the `comparator` method is left unimplemented in Backbone.js. The code below sorts the library by book title:
|
|
190
|
+
|
|
191
|
+
**Extending a collection**
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
exports.definition = {
|
|
195
|
+
config : { // table schema and adapter information
|
|
196
|
+
},
|
|
197
|
+
extendModel(Model) {
|
|
198
|
+
_.extend(Model.prototype, { // Extend, override or implement Backbone.Model methods
|
|
199
|
+
});
|
|
200
|
+
return Model;
|
|
201
|
+
},
|
|
202
|
+
extendCollection(Collection) {
|
|
203
|
+
_.extend(Collection.prototype, { // Implement the comparator method.
|
|
204
|
+
comparator(book) {
|
|
205
|
+
return book.get('title');
|
|
206
|
+
}
|
|
207
|
+
}); // end extend
|
|
208
|
+
|
|
209
|
+
return Collection;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
#### Underscore.js Functionality
|
|
215
|
+
|
|
216
|
+
Also, the Backbone.Collection class inherits some functionality from [Underscore.js](https://underscorejs.org/), which can help simplify iterative functions. For example, to add the title of each book object in the library collection to a table, you could use the `map` function to set the table:
|
|
217
|
+
|
|
218
|
+
**Iterating over a collection with underscore**
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
const data = library.map(book => {
|
|
222
|
+
// The book argument is an individual model object in the collection
|
|
223
|
+
const title = book.get('title');
|
|
224
|
+
const row = Ti.UI.createTableViewRow({"title":title});
|
|
225
|
+
return row;
|
|
226
|
+
});
|
|
227
|
+
// TableView object in the view with id = 'table'
|
|
228
|
+
$.table.setData(data);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Event Handling
|
|
232
|
+
|
|
233
|
+
When working with Alloy Models and Collections, use the Backbone.Events `on`, `off` and `trigger` methods. For example:
|
|
234
|
+
|
|
235
|
+
**Using events with collections**
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
const library = Alloy.createCollection('book');
|
|
239
|
+
function event_callback (context) {
|
|
240
|
+
const output = context || 'change is bad.';
|
|
241
|
+
Ti.API.info(output);
|
|
242
|
+
};
|
|
243
|
+
// Bind the callback to the change event of the collection.
|
|
244
|
+
library.on('change', event_callback);
|
|
245
|
+
// Trigger the change event and pass context to the handler.
|
|
246
|
+
library.trigger('change', 'change is good.');
|
|
247
|
+
// Passing no parameters to the off method unbinds all event callbacks to the object.
|
|
248
|
+
library.off();
|
|
249
|
+
// This trigger does not have a response.
|
|
250
|
+
library.trigger('change');
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Alloy Model and Collection objects don't support the Titanium `addEventListener`, `removeEventListener` and `fireEvent` methods.
|
|
254
|
+
|
|
255
|
+
If you are using Alloy's Model-View binding mechanism, the Backbone add, change, destroy, fetch, remove, and reset events are automatically bound to an internal callback to update the model data in the view. Be careful not to override or unbind these events.
|
|
256
|
+
|
|
257
|
+
If you want to fire or listen to multiple events, Backbone.js uses spaces to delimit its events in the event string; therefore, do **NOT** name any custom events with spaces.
|
|
258
|
+
|
|
259
|
+
## Alloy Data Binding
|
|
260
|
+
|
|
261
|
+
### Introduction
|
|
262
|
+
|
|
263
|
+
When data in the collection changes, you may want to update the view simultaneously to keep information synchronized. This concept is known as data binding. Both Alloy and Backbone provide some mechanisms to bind model data to a view.
|
|
264
|
+
|
|
265
|
+
### Alloy Binding
|
|
266
|
+
|
|
267
|
+
In Alloy, collection data can be synchronized to a view object, or a single model can be bound to a view component. Alloy monitors the Backbone add, change, destroy, fetch, remove, and reset events to update the data in the view.
|
|
268
|
+
|
|
269
|
+
#### Collection-View Binding
|
|
270
|
+
|
|
271
|
+
To enable collection-view binding, create a global singleton or controller-specific collection using the [Collection tag](https://titaniumsdk.com/guide/Alloy_Framework/Alloy_Guide/Alloy_Views/Alloy_XML_Markup.html#collection-element) in the XML markup of the main view, then add the view object you want to bind data to. The following Titanium view objects support binding to a Collection:
|
|
272
|
+
|
|
273
|
+
| View Object | Since Alloy version | Add data binding attributes to... | Repeater Object to map model attributes to view properties |
|
|
274
|
+
| -------------- | ------------------- | ---------------------------------------------- | ------------------------------------------------------------------------------ |
|
|
275
|
+
| ButtonBar | 1.1 | `<Labels>` | `<Label/>` |
|
|
276
|
+
| CoverFlowView | 1.1 | `<Images>` | `<Image/>` |
|
|
277
|
+
| ListView | 1.2 | `<ListSection>` | `<ListItem/>` |
|
|
278
|
+
| Map Module | 1.4 | `<Module module="ti.map" method="createView">` | None, model attributes will be used as params for createAnnotation() directly. |
|
|
279
|
+
| Picker | 1.5 | `<PickerColumn>` or `<Column>` | `<PickerRow/>` or `<Row/>` |
|
|
280
|
+
| ScrollableView | 1.1 | `<ScrollableView>` | `<View/>` May contain children view objects. |
|
|
281
|
+
| TableView | 1.0 | `<TableView>` | `<TableViewRow/>` May contain children view objects. |
|
|
282
|
+
| TabbedBar | 1.1 | `<Labels>` | `<Label/>` |
|
|
283
|
+
| Toolbar | 1.1 | `<Items>` | `<Item/>` |
|
|
284
|
+
| View | 1.0 | `<View>` | Any view object except a top-level container like a Window or TabGroup |
|
|
285
|
+
|
|
286
|
+
You need to specify additional attributes in the markup, which are only specific to collection data binding. The only mandatory attribute is `dataCollection`, which specifies the collection singleton or instance to render. Note that you can only add these attributes to specific XML elements (refer to the table above).
|
|
287
|
+
|
|
288
|
+
* `dataCollection`: specifies the collection singleton or instance to bind to the table. This is the name of the model file for singletons or the ID prefixed with the controller symbol ('$') for instances.
|
|
289
|
+
|
|
290
|
+
* `dataTransform`: specifies an optional callback to use to format model attributes. The passed argument is a model and the return value is a modified model as a JSON object.
|
|
291
|
+
|
|
292
|
+
* `dataFilter`: specifies an optional callback to use to filter data in the collection. The passed argument is a collection and the return value is an array of models.
|
|
293
|
+
|
|
294
|
+
* `dataFunction`: set to an arbitrary identifier (name) for a function call. Use this identifier to call a function in the controller to manually update the view. This is not a declared function in the controller. This attribute creates an alias to access the underlying binding function, which is part of the Alloy data-view binding framework.
|
|
295
|
+
|
|
296
|
+
Next, create a repeater object (refer to the table above) and place it inline with the view object with the `dataCollection` attribute, or place it in a separate view and use the `Require` tag to import it.
|
|
297
|
+
|
|
298
|
+
To map model attributes, enclose the attribute with curly brackets or braces ('{' and '}'). You can map more than one attribute to a repeater object's property. For example, to assign the Label.text property to the model's title and author attributes, use this notation: `<Label text="{title} by {author}" />.` For more complex transformations, use the `dataTransform` callback to create a custom attribute.
|
|
299
|
+
|
|
300
|
+
In the controller code of the repeater object, you can use the special variable `$model` to reference the current model being iterated over. This variable is present only in data bound controllers and is a reference to the currently bound model. For example, to get the title attribute of the current model, use `$model.title` to access it.
|
|
301
|
+
|
|
302
|
+
> **⚠️ ⚠️ Warning**
|
|
303
|
+
> **IMPORTANT:** When using Alloy's data binding in a view-controller, you **MUST** call the `$.destroy()` function when closing a controller to prevent potential memory leaks. The `destroy` function unbinds the callbacks created by Alloy when the collection-view syntax is used. For example:
|
|
304
|
+
>
|
|
305
|
+
> ```javascript
|
|
306
|
+
> $.win.addEventListener("close", () => {
|
|
307
|
+
> $.destroy();
|
|
308
|
+
> });
|
|
309
|
+
> ```
|
|
310
|
+
>
|
|
311
|
+
> For global singletons, to properly release them you should also remove event handlers with `off()` and set the reference to null:
|
|
312
|
+
>
|
|
313
|
+
> ```javascript
|
|
314
|
+
> $.win.addEventListener("close", () => {
|
|
315
|
+
> $.destroy();
|
|
316
|
+
> Alloy.Collections.book.off();
|
|
317
|
+
> Alloy.Collections.book = null;
|
|
318
|
+
> });
|
|
319
|
+
> ```
|
|
320
|
+
|
|
321
|
+
#### Collection-View Binding Example
|
|
322
|
+
|
|
323
|
+
The following example demonstrates how to add basic collection-view binding to an application. The example binds a collection of album models to a ScrollableView. In the ScrollableView, each model has its own view, which displays the album cover, title of the album and the artist. The `artist` and `title` attributes are bound to a Label object and the `cover` attribute is bound to an ImageView object.
|
|
324
|
+
|
|
325
|
+
1. Add the `<Collection>` tag as a child of the `<Alloy>` tag.
|
|
326
|
+
|
|
327
|
+
**app/views/index.xml**
|
|
328
|
+
|
|
329
|
+
```xml
|
|
330
|
+
<Alloy>
|
|
331
|
+
<Collection src="album" />
|
|
332
|
+
</Alloy>
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
2. Next, add the view object(s) you want to bind the data to. In this example, data will be bound to a ScrollableView object.
|
|
336
|
+
|
|
337
|
+
**app/views/index.xml**
|
|
338
|
+
|
|
339
|
+
```xml
|
|
340
|
+
<Alloy>
|
|
341
|
+
<Collection src="album" />
|
|
342
|
+
<Window backgroundColor="white" onClose="cleanup">
|
|
343
|
+
<ScrollableView></ScrollableView>
|
|
344
|
+
</Window>
|
|
345
|
+
</Alloy>
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
3. Add the `dataCollection` attribute to the appropriate view object. Assign this attribute to the collection you want to use. For a ScrollableView object, add the attribute to the `<ScrollableView>` tag. The element to add the attribute to depends on which view object you want to bind data to.
|
|
349
|
+
|
|
350
|
+
**app/views/index.xml**
|
|
351
|
+
|
|
352
|
+
```xml
|
|
353
|
+
<Alloy>
|
|
354
|
+
<Collection src="album" />
|
|
355
|
+
<Window backgroundColor="white" onClose="cleanup">
|
|
356
|
+
<ScrollableView dataCollection="album"></ScrollableView>
|
|
357
|
+
</Window>
|
|
358
|
+
</Alloy>
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
4. Next, create your repeater object and add model attributes. Enclose the model attributes with curly brackets or braces ('{' and '}'). For a ScrollableView, the repeater object can be a View object with additional children objects. The repeater object depends on which view object you are using.
|
|
362
|
+
|
|
363
|
+
**app/views/index.xml**
|
|
364
|
+
|
|
365
|
+
```xml
|
|
366
|
+
<Alloy>
|
|
367
|
+
<Collection src="album"/>
|
|
368
|
+
<Window backgroundColor="white" onClose="cleanup">
|
|
369
|
+
<ScrollableView dataCollection="album">
|
|
370
|
+
<View layout="vertical">
|
|
371
|
+
<ImageView image="{cover}" />
|
|
372
|
+
<Label text="{title} by {artist}" />
|
|
373
|
+
</View>
|
|
374
|
+
</ScrollableView>
|
|
375
|
+
</Window>
|
|
376
|
+
</Alloy>
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
5. In the controller, call the Collection's `fetch()` method to initialize the collection and sync any stored models to the view. Remember to call the `$.destroy()` method when you close the controller to prevent memory leaks.
|
|
380
|
+
|
|
381
|
+
**app/controllers/index.js**
|
|
382
|
+
|
|
383
|
+
```javascript
|
|
384
|
+
$.index.open();
|
|
385
|
+
Alloy.Collections.album.fetch();
|
|
386
|
+
|
|
387
|
+
function cleanup() {
|
|
388
|
+
$.destroy();
|
|
389
|
+
}
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
The application is now setup for basic collection-view binding. When any new data is added to the collection, the ScrollableView will be updated with the new data.
|
|
393
|
+
|
|
394
|
+
#### Model-View Binding
|
|
395
|
+
|
|
396
|
+
To bind a single model to a component, create a global singleton or controller-specific model using the [Model tag](https://titaniumsdk.com/guide/Alloy_Framework/Alloy_Guide/Alloy_Views/Alloy_XML_Markup.html#model-element) in the XML markup of the main view and map the model attribute to the view component. To map the attribute to the view component, prefix the model name or id to the attribute, then enclose it with curly brackets or braces ('{' and '}').
|
|
397
|
+
|
|
398
|
+
To do complex transformations on the model attributes, extend the model prototype with a `transform()` function. It should return the modified model as a JSON object.
|
|
399
|
+
|
|
400
|
+
**app/models/album.js**
|
|
401
|
+
|
|
402
|
+
```javascript
|
|
403
|
+
exports.definition = {
|
|
404
|
+
config: {}, // model definition
|
|
405
|
+
extendModel(Model) {
|
|
406
|
+
_.extend(Model.prototype, {
|
|
407
|
+
transform() {
|
|
408
|
+
const transformed = this.toJSON();
|
|
409
|
+
transformed.artist = transformed.artist.toUpperCase();
|
|
410
|
+
return transformed;
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
return Model;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
A global singleton instance is a single instance of a particular model that is available for use anywhere in your application. When using global instances, they will be in memory for the duration of your application unless you manually release them. The process of manually releasing them should include:
|
|
419
|
+
|
|
420
|
+
* If any controllers are using data binding that relies on the global instance, they should call their own destroy() function: `$.destroy()`
|
|
421
|
+
* Any other event handlers added to the global instance should be removed with the [off()](http://backbonejs.org/#Events-off) function
|
|
422
|
+
* Set the reference of the model to null: `Alloy.Models.nameOfModel = null;`
|
|
423
|
+
|
|
424
|
+
Note that you need to call the `$.destroy()` function when closing the controller to prevent potential memory leaks. The `destroy` function unbinds the callbacks created by Alloy when the model-view syntax is used.
|
|
425
|
+
|
|
426
|
+
#### Model-View Binding Example
|
|
427
|
+
|
|
428
|
+
The example below demonstrates how to bind a model to view components in the XML markup. Notice that each attribute is prefixed with the model's name and enclosed with braces.
|
|
429
|
+
|
|
430
|
+
```xml
|
|
431
|
+
<Alloy>
|
|
432
|
+
<Model src="settings"/>
|
|
433
|
+
<Window backgroundColor="white" onClose="cleanup">
|
|
434
|
+
<View layout="vertical">
|
|
435
|
+
<Label text="Text Size" />
|
|
436
|
+
<Slider value="{settings.textsize}" max="5" min="1"/>
|
|
437
|
+
<Label text="Bold"/>
|
|
438
|
+
<Switch value="{settings.bold}" />
|
|
439
|
+
<Label text="Italics"/>
|
|
440
|
+
<Switch value="{settings.italics}" />
|
|
441
|
+
</View>
|
|
442
|
+
</Window>
|
|
443
|
+
</Alloy>
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
#### Collection Example
|
|
447
|
+
|
|
448
|
+
The example below demonstrates how to display all book models in the collection by the author Mark Twain. It also demonstrates how to use each of the data binding attributes.
|
|
449
|
+
|
|
450
|
+
**app/views/index.xml**
|
|
451
|
+
|
|
452
|
+
```xml
|
|
453
|
+
<Alloy>
|
|
454
|
+
<Collection src="book" />
|
|
455
|
+
<Window class="container">
|
|
456
|
+
<TableView dataCollection="book"
|
|
457
|
+
dataTransform="transformFunction"
|
|
458
|
+
dataFilter="filterFunction"
|
|
459
|
+
dataFunction="updateUI"
|
|
460
|
+
onDragEnd="refreshTable">
|
|
461
|
+
<!-- Also can use Require -->
|
|
462
|
+
<TableViewRow title="{title}" />
|
|
463
|
+
</TableView>
|
|
464
|
+
</Window>
|
|
465
|
+
</Alloy>
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
**app/controllers/index.js**
|
|
469
|
+
|
|
470
|
+
```javascript
|
|
471
|
+
$.index.open();
|
|
472
|
+
|
|
473
|
+
// Encase the title attribute in square brackets
|
|
474
|
+
function transformFunction(model) {
|
|
475
|
+
// Need to convert the model to a JSON object
|
|
476
|
+
const transform = model.toJSON();
|
|
477
|
+
transform.title = '[' + transform.title + ']';
|
|
478
|
+
// Example of creating a custom attribute, reference in the view using {custom}
|
|
479
|
+
transform.custom = transform.title + " by " + transform.author;
|
|
480
|
+
return transform;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Show only book models by Mark Twain
|
|
484
|
+
function filterFunction(collection) {
|
|
485
|
+
return collection.where({author:'Mark Twain'});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function refreshTable(){
|
|
489
|
+
// Trigger the binding function identified by the dataFunction attribute
|
|
490
|
+
updateUI();
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Trigger the synchronization
|
|
494
|
+
const library = Alloy.Collections.book;
|
|
495
|
+
library.fetch();
|
|
496
|
+
|
|
497
|
+
// Free model-view data binding resources when this view-controller closes
|
|
498
|
+
$.index.addEventListener('close', () => {
|
|
499
|
+
$.destroy();
|
|
500
|
+
});
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
As the collection is updated, the view reflects the changes made to the models. If you want to suppress an update, specify `{silent: true}` in the `options` parameters when calling Backbone methods to change model data.
|
|
504
|
+
|
|
505
|
+
### Collection vs Model Data Binding
|
|
506
|
+
|
|
507
|
+
You can bind both a collection of models or an individual model. To bind a model attribute the opening curly bracket is first followed by the model name and then the attribute. To bind a collection you add the `dataCollection` attribute to the container using the collection name as value. The generated code will then loop over the collection and add the child elements to the container for each model.
|
|
508
|
+
|
|
509
|
+
```xml
|
|
510
|
+
<Alloy>
|
|
511
|
+
<Model src="currentCategory" />
|
|
512
|
+
<Collection src="book" />
|
|
513
|
+
<Window>
|
|
514
|
+
<!-- model data binding -->
|
|
515
|
+
<Label text="{currentCategory.name}" />
|
|
516
|
+
|
|
517
|
+
<!-- collection data binding -->
|
|
518
|
+
<ScrollView dataCollection="book">
|
|
519
|
+
<Label text="{title}" />
|
|
520
|
+
</ScrollView>
|
|
521
|
+
</Window>
|
|
522
|
+
</Alloy>
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
### Global Singleton vs Local Instance
|
|
526
|
+
|
|
527
|
+
In the above code snippet, the model and collection are global singletons under `Alloy.Models.currentCategory` and `Alloy.Collections.book`. You can also use local instances for the current controller by adding `instance="true"` as attribute. You also need to assign them an ID to reference them in the XML and controller.
|
|
528
|
+
|
|
529
|
+
```xml
|
|
530
|
+
<Alloy>
|
|
531
|
+
<Model src="currentCategory" instance="true" id="c" />
|
|
532
|
+
<Collection src="book" instance="true" id="b" />
|
|
533
|
+
<Window>
|
|
534
|
+
<!-- model data binding -->
|
|
535
|
+
<Label text="{$.c.name}" />
|
|
536
|
+
|
|
537
|
+
<!-- collection data binding -->
|
|
538
|
+
<ScrollView dataCollection="$.b">
|
|
539
|
+
<Label text="{title}" />
|
|
540
|
+
</ScrollView>
|
|
541
|
+
</Window>
|
|
542
|
+
</Alloy>
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
### Simple vs Complex Data Binding
|
|
546
|
+
|
|
547
|
+
It's important to understand the difference between simple and complex data binding as they were implemented in unique ways which results in different behaviour.
|
|
548
|
+
|
|
549
|
+
Simple data binding involves one model attribute where complex data binding involves a combination of strings (including white space) and model attributes or even multiple model attributes:
|
|
550
|
+
|
|
551
|
+
```xml
|
|
552
|
+
<Alloy>
|
|
553
|
+
<Model src="book">
|
|
554
|
+
<Window>
|
|
555
|
+
<!-- simple -->
|
|
556
|
+
<Label text="{book.title}" />
|
|
557
|
+
|
|
558
|
+
<!-- complex -->
|
|
559
|
+
<Label text="Title: {book.title}" />
|
|
560
|
+
<Label text="{book.author.name} {book.author.email}" />
|
|
561
|
+
</Window>
|
|
562
|
+
</Alloy>
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
### Backbone Binding
|
|
566
|
+
|
|
567
|
+
The application can monitor Backbone events to trigger updates to the view.
|
|
568
|
+
|
|
569
|
+
For instance, the code below demonstrates how to update a table when a model object is added to a collection by monitoring the add event:
|
|
570
|
+
|
|
571
|
+
```javascript
|
|
572
|
+
library.on('add', e => {
|
|
573
|
+
// custom function to update the content on the view
|
|
574
|
+
updateFooView(library);
|
|
575
|
+
});
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
Another method is to selectively monitor changes. For instance, the code below demonstrates how to update data if a title changes in the collection:
|
|
579
|
+
|
|
580
|
+
```javascript
|
|
581
|
+
library.on('change:title', e => {
|
|
582
|
+
// custom function to update the content on the view
|
|
583
|
+
updateFooView(library);
|
|
584
|
+
});
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
> **⚠️ ⚠️ Warning**
|
|
588
|
+
> This only works if the Backbone method fires the change event and does not enable `{silent: true}` as an option.
|
|
589
|
+
|
|
590
|
+
If you want to suppress an update, specify `{silent: true}` in the `options` parameters when calling Backbone methods to change model data. The data-bound view will not be updated to reflect the changes.
|
|
591
|
+
|
|
592
|
+
### Bind Deep Object Properties
|
|
593
|
+
|
|
594
|
+
You can bind deep object properties:
|
|
595
|
+
|
|
596
|
+
```xml
|
|
597
|
+
<Alloy>
|
|
598
|
+
<Model src="book" />
|
|
599
|
+
<Label text="{book.author.name}" />
|
|
600
|
+
</Alloy>
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
Before, you needed to use a transformer to create a reference like `authorName`.
|
|
604
|
+
|
|
605
|
+
Before CLI 7.1.0, the only way to set object properties (e.g. `font.fontFamily` for a Label) was to use TSS. You can use dot notation in XML:
|
|
606
|
+
|
|
607
|
+
```xml
|
|
608
|
+
<Alloy>
|
|
609
|
+
<Model src="book" />
|
|
610
|
+
<Label font.fontFamily="Roboto">Hello</Label>
|
|
611
|
+
</Alloy>
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
### Use Models and Properties with Special Characters
|
|
615
|
+
|
|
616
|
+
You can bind models and properties that use names with special characters like dashes and spaces. Simply wrap the names in square brackets and quotes like you'd do in JavaScript:
|
|
617
|
+
|
|
618
|
+
```xml
|
|
619
|
+
<Alloy>
|
|
620
|
+
<Model src="my-model">
|
|
621
|
+
<Label text="['my-model']['my-property']" />
|
|
622
|
+
</Alloy>
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
### Bind Multiple Models to the Same View
|
|
626
|
+
|
|
627
|
+
You have the ability to bind multiple models to the same view:
|
|
628
|
+
|
|
629
|
+
```xml
|
|
630
|
+
<Alloy>
|
|
631
|
+
<Model src="a" />
|
|
632
|
+
<Model src="b" />
|
|
633
|
+
<Label text="{a.hello} {b.world}" />
|
|
634
|
+
</Alloy>
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
### Define Transformations in the Model
|
|
638
|
+
|
|
639
|
+
Since Alloy 1.8.1, all types of data binding will generate the following logic to determine what object will be bound to the view:
|
|
640
|
+
|
|
641
|
+
```javascript
|
|
642
|
+
let t;
|
|
643
|
+
if (_.isFunction(<dataTransform>)) { // only for collection binding
|
|
644
|
+
t = <dataTransform>(model);
|
|
645
|
+
} else if (_.isFunction(model.transform)) {
|
|
646
|
+
t = model.transform();
|
|
647
|
+
} else {
|
|
648
|
+
t = model.toJSON();
|
|
649
|
+
}
|
|
650
|
+
$.myLabel.text = t.author.name;
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
You'd extend a model with a `transform()` method as such:
|
|
654
|
+
|
|
655
|
+
```javascript
|
|
656
|
+
exports.definition = {
|
|
657
|
+
// config
|
|
658
|
+
extendModel(Model) {
|
|
659
|
+
_.extend(Model.prototype, {
|
|
660
|
+
transform() {
|
|
661
|
+
const t = this.toJSON();
|
|
662
|
+
t.titleCaps = t.title.toUpperCase();
|
|
663
|
+
return t;
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
return Model;
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
> **⚠️ ⚠️ Warning**
|
|
672
|
+
> The `transform` method must return **all** bound properties, not just the transformed ones. Until Alloy 1.8.1, simple collection data binding did not require this and automatically fell back to the model attributes.
|
|
673
|
+
|
|
674
|
+
### Tips and Tricks
|
|
675
|
+
|
|
676
|
+
#### Lazy Transformation
|
|
677
|
+
|
|
678
|
+
The advantage of defining transformations in the model is that you don't need to repeat them in every controller. A possible disadvantage is that everywhere you bind the model all transformations are computed where you might only need some.
|
|
679
|
+
|
|
680
|
+
You can handle this using `Object.defineProperty()`. Its `get` callback will only be called when the transform key is actually requested:
|
|
681
|
+
|
|
682
|
+
```javascript
|
|
683
|
+
const moment = require('alloy/moment');
|
|
684
|
+
|
|
685
|
+
exports.definition = {
|
|
686
|
+
extendModel(Model) {
|
|
687
|
+
_.extend(Model.prototype, {
|
|
688
|
+
transform() {
|
|
689
|
+
const model = this;
|
|
690
|
+
const t = this.toJSON();
|
|
691
|
+
|
|
692
|
+
Object.defineProperty(t, 'dateFormatted', {
|
|
693
|
+
get() {
|
|
694
|
+
return moment(t.date).format('LLLL');
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
return t;
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
return Model;
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
#### Populating a Model After Data Binding
|
|
707
|
+
|
|
708
|
+
When Alloy compiles your views and controllers, the generated view code precedes your controller code. Any models you define for data binding in the XML will also be created at that point. Just like you call `fetch()` to populate the collection, you do the exact same thing for the model.
|
|
709
|
+
|
|
710
|
+
**index.xml**
|
|
711
|
+
|
|
712
|
+
```xml
|
|
713
|
+
<Alloy>
|
|
714
|
+
<Model src="book" instance="true" id="current" />
|
|
715
|
+
<Window>
|
|
716
|
+
<Label text="{book.title}" />
|
|
717
|
+
</Window>
|
|
718
|
+
</Alloy>
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
**index.js**
|
|
722
|
+
|
|
723
|
+
```javascript
|
|
724
|
+
$.current.fetch({
|
|
725
|
+
id: Ti.App.Properties.getString('currentBook')
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
$.index.open();
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
> **⚠️ ⚠️ Warning**
|
|
732
|
+
> With the release of CLI 7.1.0, values passed in at creation of a view can be used as values in TSS and XML. For example, if the **foo** property was passed in at creation it can be used on a label:
|
|
733
|
+
>
|
|
734
|
+
> ```xml
|
|
735
|
+
> <Alloy>
|
|
736
|
+
> <Label text="$.args.foo" />
|
|
737
|
+
> </Alloy>
|
|
738
|
+
> ```
|
|
739
|
+
|
|
740
|
+
---
|
|
741
|
+
|
|
742
|
+
For sync adapters, custom adapters, migrations, plain Backbone usage, and the Backbone version migration (0.9.2 → 1.1.2 / 1.3.3), continue with [MODELS_ADVANCED.md](MODELS_ADVANCED.md).
|