1

Alloyフレームワークで、xmlファイルで定義されたビューを動的に追加したいが、他のビューにバインドされていない。

次の例を見てみましょう。

index.xmlに入力したいコンテナ:

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

ScrollableViewに入るビューごとにインスタンス化するビューテンプレートquestion.xml:

<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を使用したい...

どんな助けでも大歓迎です!

4

1 に答える 1

3

モデルオブジェクトを渡しますが、代わりに次のようなJSONオブジェクトを渡します。

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

または、データバインディングと属性を使用して、xmlファイル内でこれをすべて実行dataCollectionし、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

于 2013-03-26T20:02:05.240 に答える