繁体   English   中英

如何在钛/合金中使用localstorage?

[英]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);

欲获得更多信息:

Titanium Sync和apadters

Backbone Sync,集合,模型等

有关一般指南,请参阅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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM