config, defines the table schema and adapter information. The next two objects
extendCollection define functions to extend, override or implement the Backbone.Model and Backbone.Collection classes, respectively.
To access a model locally in a controller, use 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.
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:
For examples using markup, see Alloy XML Markup: Model Element.
config object is comprised of three different objects:
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:
boolean. By default, any unknown data type maps to the SQLite type
TEXT. Alternatively, the SQLite sync adapter accepts the SQLite keywords.
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.
The adapter object defines how to access persistent storage. It contains two key-value pairs:
type key identifies the sync adapter and the
collection_name key identifies the name of the table in the database or a namespace. See Alloy Sync Adapters and Migrations for more information.
For example, suppose there is a model object called book (
book.js) defined as:
The code above describes a book object, which has two
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".
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. See Alloy Sync Adapters and Migrations: Custom Sync Adapters for more information.
Extending the Backbone.Model class
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.
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
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.
In the example code
In the controller, to access the model, do:
For more details, see the Backbone.Model API.
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
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.
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:
For examples using markup, see Alloy XML Markup: Collection Element.
Extending the Backbone.Collection class
comparator method is left unimplemented in Backbone.js. The code below sorts the library by book title:
Additionally, the Backbone.Collection class inherits some functionality from Underscore.js, 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:
For more details, see the Backbone.Collection API.
When working with Alloy Models and Collections, use the Backbone.Events
trigger methods. For example:
Alloy Model and Collection objects don't support the Titanium
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.
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.
For more details, see the Backbone.Events API.