Reputation: 28951
I have 4 views all with a single button. I want the following simple behaviour.
This is what I have so far:
home
<Alloy>
<Window class="container">
<Button onClick="openProfile">Open Dashboard</Button>
</Window>
</Alloy>
With JS:
function openDashboard() {
Alloy.createController('dashboard').getView();
}
dashboard
<Alloy>
<Window class="container">
<Button onClick="openProfile">Open Profile</Button>
</Window>
</Alloy>
With JS:
function openProfile() {
Alloy.createController('profile').getView();
}
profile
<Alloy>
<Window class="container">
<Button onClick="openSettings">Open Settings</Button>
</Window>
</Alloy>
With JS:
function openSettings() {
Alloy.createController('settings').getView();
}
settings
<Alloy>
<Window class="container">
<Button onClick="backToHome">Back To Home</Button>
</Window>
</Alloy>
However I am unsure on the JS to use to close both parent windows.
Upvotes: 0
Views: 298
Reputation: 28951
The way I managed to do it is by passing the parent windows across like so:
home
function openDashboard() {
Alloy.createController('dashboard', {'parent': $}).getView();
}
dashboard
function openProfile() {
Alloy.createController('profile', {'parent': $}).getView();
}
profile
function openSettings() {
Alloy.createController('settings', {'parent': $}).getView();
}
settings
function backToHome() {
$.getView().close(); // Close current window
$.args.parent.getView().close(); // Close profile window
$.args.parent.args.parent.getView().close(); // Close dashboard
}
Loops can make the above code more readable like so:
function backToHome() {
var controller = $;
for (var i = 0; i < 3; i++) {
controller.getView().close();
controller = controller.args.parent;
}
}
Upvotes: 0
Reputation: 3539
There are 2 best ways to do this:
Method 1: Using JS-only event dispatcher system.
Create and put a file 'EventDispatcher.js' in app-lib folder and put this code in it.
module.exports = _.clone(Backbone.Events)
Put this code in dashboard.js & profile.js
var eventsDispatcher = require('EventDispatcher');
eventsDispatcher.on('closeEventFromSettings', closeMethod);
function closeMethod() {
eventsDispatcher.off('closeEventFromSettings', closeMethod);
$.getView().close();
}
Finally use this code on settings.js whenever you want to close dashboard and profile windows
require('EventDispatcher').trigger('closeEventFromSettings');
Method 2: Passing callbacks to window controller.
dashboard.js
function closeMe() {
$.dashboard.close();
}
function openProfile() {
Alloy.createController('profile', {cb: closeMe}).getView();
}
profile.js
function closeMe() {
$.args.cb(); // it will close the dashboard
$.profile.close();
}
function openSettings() {
Alloy.createController('settings', {cb: closeMe}).getView();
}
settings.js
function backToHome() {
$.args.cb();
}
There are other ways obviously, but I prefer to use these 2 only from performance and maintenance perspective.
Upvotes: 1