2014-12-19 53 views
0

我把我的所有项目编码在Alloy上,所以这里没有经典的Titanium。 我想在我的索引中加载一个外部定制AlertDialog(位于views/popup.xml中)。所以我需要通过点击确定按钮来显示警报并销毁它(例如)。帮助按钮应该执行另一个操作。钛:如何加载合金中的外部自定义AlertDialog?

我popup.xml文件:

<Alloy> 
    <AlertDialog id="popup" title="Error popup" 
     message="There is an error" cancel="1"> 
     <ButtonNames> 
      <ButtonName>OK</ButtonName> 
      <ButtonName>Help</ButtonName> 
     </ButtonNames> 
    </AlertDialog> 
</Alloy> 

我index.js文件:

function openPopup(e) { 
    var page = Alloy.createController('views/popup').getView(); 
    page.show(); 
}; 
openPopup(); 

但是,这给了我一个错误:

  • [DEBUG] [iphone,8.1 ,192.168.0.1]原生 模块:alloy/controllers/views/popup
  • [错误] [iphone,8.1,192.168.0.1]找不到模块: alloy/controllers/views/popup
  • [错误] [iphone,8.1,192.168.0.1] TypeError:'undefined'is not a 构造函数(计算 '新(__p.require( “合金/控制器/” + 名))(参数)')

我没有popup.js,我并不需要在索引的任何文件。 js也是。所以我的问题是:如何动态加载控制器?如何使用addEventListener在“单击”操作上删除(或销毁它)?谢谢。

+1

只使用'Alloy.createController( '弹出')getView();','没有意见/ popup' – turtle 2014-12-19 06:09:26

+0

它的工作原理!谢谢 ! – 2014-12-19 08:29:58

回答

0

同意乌龟。当你创建一个控制器时,它隐含地知道你指的是app/views目录中的一个视图。

而且您应该通过不创建局部变量(用于垃圾收集目的)来稍微改进您的代码。因此,而不是:

var page = Alloy.createController('views/popup').getView(); 
page.show(); 

你应该只是做:

Alloy.createController('views/popup').getView().open(); 

您可以找到有关这在splendid article by Fokke Zandbergen更多信息。

/约翰

+0

谢谢你的诀窍,约翰! – 2014-12-22 10:52:04