Created
February 18, 2014 15:44
-
-
Save davidecassenti/9073463 to your computer and use it in GitHub Desktop.
Appcelerator Alloy | Using a common library that can access multiple Alloy views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HomeLibrary = require('home_library'); | |
// HomeController is the name that will be used inside | |
// the XML view to access the methods of the library | |
var HomeController = new HomeLibrary('home1', $); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Alloy> | |
<Window id="home" backgroundColor="yellow"> | |
<Label id="mylabel" top="30">Home 1</Label> | |
<Button id="mybutton" onClick="HomeController.button" top="60">Click</Button> | |
<Button id="close" onClick="HomeController.close" top="90">Close</Button> | |
</Window> | |
</Alloy> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HomeLibrary = require('home_library'); | |
var HomeController = new HomeLibrary('home2', $); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Alloy> | |
<Window id="home" backgroundColor="pink"> | |
<Label id="mylabel" top="30">Home 2</Label> | |
<Button id="close" onClick="HomeController.close" top="60">Close</Button> | |
</Window> | |
</Alloy> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NOTE: this file is in the /lib/ directory | |
// | |
var $ = null; // redefine the $, so that we use the specific subview UI elements | |
var controller = null; | |
// Constructor | |
function HomeLibrary(_controller, _$) { | |
// TODO should make sure that the parameters are passed | |
$ = _$; // replace the $ | |
controller = _controller; // get the controller name | |
} | |
// This function will be used in the view home1.xml only | |
HomeLibrary.prototype.button = function() { | |
alert('Clicked!'); | |
}; | |
// This function will be used by both views | |
HomeLibrary.prototype.close = function() { | |
// since $ is redefined, we can access the UI views as usual | |
$.home.close(); | |
}; | |
module.exports = HomeLibrary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.index.open(); | |
// open the controller home1 | |
function open1() { | |
var home = Alloy.createController('home1').getView('home'); | |
home.open(); | |
} | |
// open the controller home2 | |
function open2() { | |
var home = Alloy.createController('home2').getView('home'); | |
home.open(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Alloy> | |
<Window> | |
<Button onClick="open1" top="30">Open 1</Button> | |
<Button onClick="open2" top="60">Open 2</Button> | |
</Window> | |
</Alloy> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment