I have 3 views: v1.xml, v2.xml, v3.xml. v1.xml has a button B1. When user clicks button B1, it opens v2.xml. v2.xml has another button B2. When user clicks button B2, it opens v3.xml.
I created a window in v1.xml. When I click on button B2, I want to add view v3 to the existing window and I do not want to create a new window. I am unable to use the window reference that I created in v1.xml as I am working in v2.xml file now.
My code:
V1.xml
<Alloy>
<Window id="main_window">
<View id="v1">
<Button id="B1" />
</View>
</Window>
</Alloy>
V2.xml
<Alloy>
<View id="v2">
<Button id="B2" />
</View>
</Alloy>
V3.xml
<Alloy>
<View id="v3">
<Label id="l3" text="test label"/>
</View>
</Alloy>
V1.js
$b1.addEventListener('click', function() {
var view = Alloy.createController("V2").getView();
$.main_window.remove(v1);
$.main_window.add(view);
});
V2.js
$.b2.addEventListener('click', function(){
var view = Alloy.createController("V3").getView();
//Add this view to main_window
});
You have to pass referance to window created in V1 when you are creating V2 controller:
V1.js
$b1.addEventListener('click', function() { var view = Alloy.createController("V2", { main_window: $.main_window }).getView(); $.main_window.remove(v1); $.main_window.add(view); });
V2.js
var args = arguments[0] || {}; var main_window = args.main_window; $.b2.addEventListener('click', function(){ var view = Alloy.createController("V3").getView(); main_window.add(view); });
Or you can create just one event listener and pass it as argument:
V1.js
var currentView; var replaceViews = function(view) { currentView && $.main_window.remove(currentView); $.main_window.add(view); currentView = view; } $.b1.addEventListener('click', function() { var view = Alloy.createController('V2', { delegate: replaceViews }).getView(); replaceViews(view); });
V2.js
var args = arguments[0] || {}; var replaceViews = args.delegate; $.b2.addEventListener('click', function() { var view = Alloy.createController('V3').getView(); replaceViews(view); });
The last option would be setting $.main_window to Alloy.Globals and creating global function.
Here you can read more about passing arguments to Alloy Controller and Alloy.Globals .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.