Update: The pull request for this feature has been accepted.
If you require a widget in a XML view like this:
1 |
<widget src="nl.fokkezb.tweetsView" foo="bar" id="myWidget"></widget> |
It generates code like this:
1 2 3 4 |
$.__views.myWidget = Alloy.createWidget("nl.fokkezb.tweetsView", "widget", { id: "myWidget", foo: "bar" }); |
Let’s take a look at the code for createWidget in alloy.js:
1 2 3 |
exports.createWidget = function(id, name, args) { return new (require("alloy/widgets/" + id + "/controllers/" + (name || "widget")))(args); }; |
Multiple view-controllers
As you can see, the second argument specifies the controller to require from the widget. You might not have known this, but the docs do mention that widget can contain multiple view-controllers, just like your app does.
Use cases
Unfortunately, to use this feature you need to create the widget in your controller using code similar to the second fragment above. It’s currently not possible to do this in the view.
This is a missed opportunity to use a singe widget to expose a collection of reusable UI elements:
1 2 3 4 5 6 |
<view> <widget src="nl.fokkezb.ui" name="myFancyButton"></widget> <widget src="nl.fokkezb.ui" name="myCustomView"></widget> <widget src="nl.fokkezb.ui" name="myHorizontalTable"></widget> … </view> |
Or create a widget that can be used in multiple contexts:
1 2 3 4 5 6 |
<tabgroup> <widget src="nl.fokkezb.tweetsView" name="tab"></widget> <tab> <widget src="nl.fokkezb.tweetsView" name="window"></widget> </tab> </tabgroup> |
Feature & pull request
So my feature request is to support the name attribute for <Widget> elements, whose value would be used as the second argument for createWidget. Since Tony’s pretty much nocked out after working hard on Alloy month after month, I’ve already made the necessary changes to Alloy.Require.js. Here it is: