如何在Appcelerator中更改DB集合时自动更新/同步ListView UI?

时间:2017-09-19 09:06:34

标签: collections model appcelerator

我有一个ListView,它从sql表中获取FAQ数据(在从服务器下载到数据库之后)。 FAQ数据被下载并正确保存在数据库中(下面保存的常见问题解答显示了服务器上发生更改时正确,更新的常见问题解答),但ListView UI不显示更改。正确,更新的常见问题解答仅在应用重新启动时显示在ListView中。

当用户刷新/重新加载时,如何更新Listview 自动

faqs.xml

<Collection src="faqCollection" instance="true" id="faqData"/>
.
.

<ListView id="faqListView" defaultItemTemplate="plainTemplate">
    <RefreshControl id="refresh" onRefreshstart="reloadFAQs" platform="ios"/>
    <Templates>
        <ItemTemplate name="plainTemplate" id="plainTemplate">
            <View width="98%" class="listViewRow">
                <Label bindId="title" id="title"/>
            </View>
        </ItemTemplate>
    </Templates>
    <ListSection id="faqListSection" dataCollection="$.faqData" >
        <ListItem title:text="{faq_title}" nid:itemId="{faq_nid}" mainText:text="{faq_text}" template="{template}"/>
    </ListSection>  
</ListView>

faqs.js

function autoSaveFAQs(faqs){
 $.faqData.deleteAll();
 console.log("faqs in autosave: " + JSON.stringify(faqs));
 var f;
 _.each(faqs, function(faq){
        faq.time = now; //adds the timestamp when FAQs have been downloaded
        f = Alloy.createModel('faqCollection', faq, {silent: true}); 
        $.faqData.add(f);
        f.save();
 });
 var savedFAQs = $.faqData;
 savedFAQs.fetch();
 console.log("savedFAQs: " + JSON.stringify(savedFAQs)); //shows the updated FAQs when reloaded, but the listview is not populated
}

1 个答案:

答案 0 :(得分:0)

<强> SOLUTION:

获取数据时有一个$ .destroy()方法:

$.faqData.on('fetch', function() {
if (OS_IOS) { 
 $.refresh.endRefreshing(); //only works on Android with AppC SDK 6.2
}
//  $.destroy(); //too much cleaning. updates automatically when not destroyed after refreshEnd
});