2013-03-26 73 views
1

在Alloy框架中,我想動態地添加一個在xml文件中定義的視圖,但不會綁定到另一個視圖。不幸的是,您的應用程序停止工作

讓我們來看看下面的例子:

集裝箱我要填寫INDEX.XML:

<ScrollableView id="scrollableBilan" showPagingControl="true"> 
</ScrollableView> 

視圖模板question.xml我想實例化每個視圖進入了ScrollableView:

<Alloy> 
    <Collection src="ReponsePossible"> 
    <View id="questionContainer" class="container"> 
     <Label id="questionText" /> 
     <Button id="buttonNextQuestion">Question suivante</Button> 
    </View> 
</Alloy> 

最後控制器index.js,質疑是一個集合實例:

for(var i=0; i<questions.length; i++){ 
     $.scrollableBilan.add(Alloy.createController('question', questions.at(i))); 
    } 

這使得我的應用程序崩潰,出現以下消息:'不幸的是,您的應用程序已停止'。我已經得到了這個錯誤,總是在試圖使用Alloy.createController動態創建視圖時添加視圖。

行爲創建與Ti.UI.createView視圖時是好的,但我想使用MVC ...

任何幫助,歡迎!

回答

3

你傳遞一個模型對象,而是試圖傳遞一個JSON對象是這樣的:

for(var i=0; i<questions.length; i++){ 
    $.scrollableBilan.add(Alloy.createController('question', questions.at(i).toJSON())); 
} 

或者你可以做這一切的XML文件中使用Data BindingdataCollection屬性,裝上去像這樣在您的INDEX.XML:

<Alloy> 
    <Collection src="questions"> 
    <ScrollableView id="scrollableBilan" showPagingControl="true" dataCollection="ReponsePossible"> 
     <View id="questionContainer" class="container"> 
      <Label id="questionText" text="{questionText}"/> 
      <Button id="buttonNextQuestion">Question suivante</Button> 
     </View> 
    </ScrollableView> 
</Alloy> 

questionText場需要在您的questions模型的屬性。

+0

再次感謝約西亞。第一個解決方案正在工但是,我不喜歡這種純粹的鈦ui元素創造和合金之間的這種混合類型。我更喜歡第二個解決方案,但是這個dataCollection綁定函數只能在下一版本的Appcelerator SDK(https://jira.appcelerator.org/browse/ALOY-451)上使用。我會等到4月15日這樣來實施它。 – krakax 2013-03-26 22:45:27

相關問題