0

我有4个视图全部用一个按钮。我想要以下简单的行为。钛/加速器合金 - 关闭多个父窗口

  • :单击该按钮应该去仪表盘。点击Android后退按钮应退出应用程序
  • 仪表板:单击该按钮应打开配置文件。单击Android后退按钮应关闭当前窗口并返回主页
  • 配置文件:单击该按钮应打开设置。点击Android后退按钮应关闭当前窗口并返回到仪表板
  • 设置:单击该按钮应关闭设置仪表盘轮廓网页,并返回用户返回到。单击Android后退按钮应只关闭当前窗口并返回配置文件

这是我到目前为止有:

<Alloy> 
    <Window class="container"> 
     <Button onClick="openProfile">Open Dashboard</Button> 
    </Window> 
</Alloy> 

随着JS:

function openDashboard() { 
    Alloy.createController('dashboard').getView(); 
} 

仪表盘

<Alloy> 
    <Window class="container"> 
     <Button onClick="openProfile">Open Profile</Button> 
    </Window> 
</Alloy> 

随着JS:

function openProfile() { 
    Alloy.createController('profile').getView(); 
} 

轮廓

<Alloy> 
    <Window class="container"> 
     <Button onClick="openSettings">Open Settings</Button> 
    </Window> 
</Alloy> 

随着JS:

function openSettings() { 
    Alloy.createController('settings').getView(); 
} 

设置

<Alloy> 
    <Window class="container"> 
     <Button onClick="backToHome">Back To Home</Button> 
    </Window> 
</Alloy> 

但是我不确定JS用于关闭两个父窗口。

回答

1

有做这2点最好的方法:

方法1:使用JS-唯一事件调度系统。

创建并放置文件'EventDispatcher。js' in app-lib文件夹并将其放入其中。

module.exports = _.clone(Backbone.Events) 

把这个代码dashboard.js & profile.js

只要你想关闭仪表板和型材窗户

require('EventDispatcher').trigger('closeEventFromSettings'); 

var eventsDispatcher = require('EventDispatcher'); eventsDispatcher.on('closeEventFromSettings', closeMethod); function closeMethod() { eventsDispatcher.off('closeEventFromSettings', closeMethod); $.getView().close(); } 

最后使用上settings.js验证码方法2:将回调传递给窗口控制器。

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(); 
} 

钍其他方式很明显,但我更喜欢仅从性能和维护角度来使用这两种方法。

+0

你怎么看待我的方式是什么?我还编辑了您的答案以删除这些图像。 –

+1

这是同样的事情,你可以传递任何东西作为回调......无论是整个控制器引用或数组或任何你喜欢的,但逻辑是传递一些东西进入它.. –

+0

也告诉我为什么我的回答是不考虑这些图片代码并为我丢出错误。我从Stack Overflow找不到任何有效的理由。 –

0

我设法做到这一点是通过将父窗口遇到像这样的方式:

function openDashboard() { 
    Alloy.createController('dashboard', {'parent': $}).getView(); 
} 

仪表盘

function openProfile() { 
    Alloy.createController('profile', {'parent': $}).getView(); 
} 

轮廓

function openSettings() { 
    Alloy.createController('settings', {'parent': $}).getView(); 
} 

设置

function backToHome() { 
    $.getView().close(); // Close current window 
    $.args.parent.getView().close(); // Close profile window 
    $.args.parent.args.parent.getView().close(); // Close dashboard 
} 

循环可以使上面的代码更易读,像这样:

function backToHome() { 
    var controller = $; 
    for (var i = 0; i < 3; i++) { 
     controller.getView().close(); 
     controller = controller.args.parent; 
    } 
}