Appcelerator存储本地搜索

时间:2016-02-23 06:23:12

标签: appcelerator appcelerator-titanium

按下按钮时,我希望本地保存按钮的ID和名称。

我不太确定解决此问题的最佳方法。我应该使用appcelerator属性(http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.App.Properties)还是将文件写入存储?目前我正在使用Ti.App.Properties.setList。

示例代码:

searchStorageName = "searchHistory";    
searchResultsArray = [];
var currentEntries = (Ti.App.Properties.getList(searchStorageName));

// Create search entry object.
var localSearchObject = {
    company_name: resultNodeCompany,
    company_id: resultNodeCompanyID,
    variation_id: resultNodeCompanyVariationID
};
// Check if existing entries, if so push current search 
// and previous searches to array.
if(currentEntries === null || currentEntries === undefined){
    searchResultsArray.push(localSearchObject); 
    Ti.App.Properties.setList(searchStorageName, searchResultsArray);
    // searchResultsArray.push(localSearchObject, currentEntries);
}
else {
    searchResultsArray.push(localSearchObject, currentEntries); 
    Ti.App.Properties.setList(searchStorageName, searchResultsArray);
}

我暂时陷入困境,因为它正在向数组中插入重复搜索。当我遍历值以在UI中创建列表时,它显示重复项。

var currentEntries = (Ti.App.Properties.getList(searchStorageName));
var currentEntriesLength = currentEntries.length;
var getPreviousHistorySearchesArray = [];
currentEntries.forEach(function(entry, index) {
   var company_name = entry.company_name;
   var company_id = entry.company_id;
   var variation_id = entry.variation_id;

   // Create View Entry.
   createSearchHistoryViewEntry(index, company_name, company_id, variation_id);
}

1 个答案:

答案 0 :(得分:1)