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.
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.