Titanium 钛合金-如何保持单体模型?

Titanium 钛合金-如何保持单体模型?,titanium,titanium-mobile,appcelerator-mobile,Titanium,Titanium Mobile,Appcelerator Mobile,在iOs和Android中运行 咖啡脚本 我有一个模型,例如: exports.definition = config: columns: cookie: "string" defaults: cookie: "" adapter: # is this valid? type

在iOs和Android中运行
咖啡脚本

我有一个模型,例如:

    exports.definition =
        config:
            columns:
                cookie: "string"
            defaults:
                cookie: ""
            adapter:
                # is this valid?
                type: "sql"
                collection_name: "userInfo"
        extendModel: (Model) ->
            _.extend Model::,
                isSignedIn:->
                    this.get('cookie').length > 0
            Model
和index.xml:

<Alloy>
    <Model id="userInfo" src="userInfo" instance="true"/>

将您的模型
userInfo.js
放入
app/model
,它可能会如下所示:

exports.definition = {

    config : {
        "columns" : {
            "cookie" : "string"
        },
        "defaults" : { "cookie" : "" }
        "adapter" : {
            "type" : "sql",
            "collection_name" : "userInfo"
        }
    },

    extendModel : function(Model) {
        _.extend(Model.prototype, {
            isSignedIn : function() {
                this.get('cookie').length > 0
            }
        });
        return Model;
    },

    extendCollection : function(Collection) {
        _.extend(Collection.prototype, {
        });
        return Collection;
    }
}
从这里开始,它取决于您想要做什么,但是您可以很容易地从集合
userInfo
中获取模型,只需将以下内容:
放在xml文件中即可

作为补充说明,我通常只使用这些东西来存储用户信息。属性用于将与应用程序相关的数据存储在属性/值对中,这些属性/值对在应用程序会话和设备电源周期之后仍然存在。例如:

// Returns the object if it exists, or null if it does not
var lastLoginUserInfo = Ti.App.Properties.getObject('userInfo', null);
if(lastLoginUserInfo === null) {
    var userInfo = {cookie : "Whatever the cookie is", id : "123456789"};
    Ti.App.Properties.setObject('userInfo', userInfo);
} else {
   // Show the cookie value of user info
   alert(lastLoginUserInfo.cookie);
}

将您的模型
userInfo.js
放入
app/model
,它可能如下所示:

exports.definition = {

    config : {
        "columns" : {
            "cookie" : "string"
        },
        "defaults" : { "cookie" : "" }
        "adapter" : {
            "type" : "sql",
            "collection_name" : "userInfo"
        }
    },

    extendModel : function(Model) {
        _.extend(Model.prototype, {
            isSignedIn : function() {
                this.get('cookie').length > 0
            }
        });
        return Model;
    },

    extendCollection : function(Collection) {
        _.extend(Collection.prototype, {
        });
        return Collection;
    }
}
从这里开始,它取决于您想要做什么,但是您可以很容易地从集合
userInfo
中获取模型,只需将以下内容:
放在xml文件中即可

作为补充说明,我通常只使用这些东西来存储用户信息。属性用于将与应用程序相关的数据存储在属性/值对中,这些属性/值对在应用程序会话和设备电源周期之后仍然存在。例如:

// Returns the object if it exists, or null if it does not
var lastLoginUserInfo = Ti.App.Properties.getObject('userInfo', null);
if(lastLoginUserInfo === null) {
    var userInfo = {cookie : "Whatever the cookie is", id : "123456789"};
    Ti.App.Properties.setObject('userInfo', userInfo);
} else {
   // Show the cookie value of user info
   alert(lastLoginUserInfo.cookie);
}

它们在appcelerator文档中没有很好地解释,但是如果您想使用内置alloy properties sync adapter存储和检索属性,则在使用模型时必须指定唯一的“id”。您已经在xml标记中这样做了:
appcelerator文档中没有很好地解释它,但是如果您想使用内置alloy properties sync adapter存储和检索属性,则在使用模型时必须指定唯一的“id”。您已经在xml标记中这样做了:
谢谢,我会记住
Ti.App.Properties
,Alloy models有一个以“Properties”响应的适配器是的,但是我发现使用Ti.App.Properties而不是适配器更容易,特别是对于单例模型,如果您只有一个,那么使用sql有什么意义呢。谢谢,我会记住,
Ti.App.Properties
,Alloy models有一个适配器,其响应为“Properties”是的,但我发现使用Ti.App.Properties而不是适配器更容易,特别是对于单例模型,如果只有一个,那么使用sql有什么意义呢。
var UserInfo = Alloy.Globals.UserInfo;