![](/img/trans.png)
[英]How to use Google Analytics in Alloy Titanium Appcelerator
[英]How to use localstorage in Titanium/Alloy?
我是Appcelerator / Titanium的新手。 誰能告訴我如何在Alloy (Titanium)中使用localstorage函數。 (沒有在Web上獲得任何好的解決方案)。
謝謝! :)
鈦合金有一個定制的實施主干。 這意味着鈦在許多方面使用Backbone,但與此同時,一些重要的功能被遺漏了。
Backbone by Titanium最常用的部分之一是模型,雖然與js框架不同,但它們有很多共同之處。
要使用數據模型,必須定義適配器(可以是localStorage,sql,屬性或自定義同步適配器)
如果您想使用localStorage,您的模型應如下所示:
exports.definition = {
config: {
"defaults": {
"first_name": "",
"last_name": "",
"phone": "",
"email": ""
},
"adapter": {
"type": 'localStorage',
"collection_name": 'user'
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
}); // end extend
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
}); // end extend
return Collection;
}
};
操縱數據然后你應該使用:
創建數據
model = Alloy.createModel('user', {first_name: 'Pedro', last_name: Picapiedra});
// or model.save();
Alloy.Collections.user.add(model);
讀數據
callection = Alloy.Collections.user.fetch()
model = Alloy.Collections.user.get(modelId)
更新數據
user.set({
first_name : 'Pablo',
last_name : 'Marmol',
});
user.save();
刪除數據
model.destroy();
collection.remove(model);
欲獲得更多信息:
有關一般指南,請參閱https://wiki.appcelerator.org/display/guides/Working+with+Local+Data+Sources 。
訪問文件是通過Ti.Filesystem完成的。 請參閱http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Filesystem上的文檔。 您還應該看到廚房水槽示例,因為它顯示熱讀/寫文件https://github.com/appcelerator/KitchenSink/blob/master/Resources/ui/common/platform/filesystem.js 。
如果您只想在本地存儲一些數據,很多人都使用sqlite數據庫。 請參閱http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Database 。
最簡單的方法是使用屬性。 它是有限的,但對許多人來說已經足夠了。 http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.App.Properties
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.