Widgets are self-contained components that can be easily dropped into an Alloy project. They were conceived as a way to reuse code in multiple projects or to be used multiple times in the same project.
Note that to use the methods list below, the correct namespace is Widget.create*
not
Alloy.Widget.create*
.
For more information on widgets, see Alloy Widgets.
Widgets are essentially miniature Alloy projects that contain their own models, views, controllers and assets. They are laid out the same way as an Alloy project.
Use Widget.createController()
, Widget.createWidget()
, Widget.createModel()
and
Widget.createCollection()
rather than the Alloy.create*
methods to create
components relative to the widget context rather than the Alloy project.
To import a widget in to a project:
app/widgets
folder. The widget must be contained within its own folder.dependencies
object in the config.json
file by adding a key/value pair with the name of
the widget as the key and the version number as the value.Add the widget to a view or create an instance of the widget in a controller:
src
attribute to the folder name of the widget.You can optionally add the id
and name
attributes to the Widget
element:
id
attribute allows you to reference the widget in the controller code. You can use this
reference to call methods exported by the widget.name
attribute allows you to import a specific view-controller in the widget rather than the
default one (widget.xml
/widget.js
). Specify the name of the view-controller minus the extension.For example, to import a widget called mywidget
in to a project, copy mywidget
to the
app/widgets
folder, where its assets, controllers, views, etc. are contained in the
app/widgets/mywidget
folder.
app
└── config.json
└── controllers
│ └── index.js
└── views
│ └── index.xml
└── widgets
└── mywidget
└── controllers
│ └── foo.js
│ └── widget.js
└── views
│ └── foo.xml
│ └── widget.xml
└── widget.json
Next, add it as a dependency in your config.json
file:
...
"dependencies":{
"mywidget":"1.0"
}
Finally, either add the widget in the XML markup of the view or create an instance of the widget in the controller.
To add the widget in the view, use the Widget
tag, specifying the src
attribute as the name of
the widget:
<Alloy>
<Window id="win">
<Widget id="myWidget" src="mywidget" />
</Window>
</Alloy>
Since the id
attribute is defined, the widget can be referenced in the controller using
$.myWidget
.
To add the widget in the controller, use the Alloy.createWidget
method. The first required parameter is
the name of the widget. The second optional parameter can specify the view component to
instantiate and the last optional parameter can specify the arguments to instantiate the widget.
For example, the following controller code is equivalent to the previous view markup example.
var myWidget = Alloy.createWidget("mywidget");
win.add(myWidget.getView());
A widget can also be added to other widgets. Follow the same procedure as above except the widget
configuration file is called widget.json
instead of config.json
.
Factory method for instantiating a Backbone collection of model objects. Creates and returns a collection for holding the named type of model objects.
See Backbone.Collection in the Backbone.js documentation for information on the methods and properties provided by the Collection object.
Name of model to hold in this collection.
Arguments to pass to the collection.
Backbone collection object.
Factory method for instantiating a controller. Creates and returns an instance of the named controller.
Name of controller to instantiate.
Arguments to pass to the controller.
Alloy controller object.
Factory method for instantiating a Backbone Model object. Creates and returns an instance of the named model.
See Backbone.Model in the Backbone.js documentation for information on the methods and properties provided by the Model object.
Name of model to instantiate.
Arguments to pass to the model.
Backbone model object.
Factory method for instantiating a widget controller. Creates and returns an instance of the named widget.
Id of widget to instantiate.
Name of the view within the widget to instantiate ('widget' by default)
Default: "widget"Arguments to pass to the widget.
Alloy widget controller object.