【问题标题】:Titanium Alloy - how to persist model singleton?钛合金 - 如何坚持模型单例?
【发布时间】:2013-01-29 22:56:24
【问题描述】:

在 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 属性在应用程序的生命周期中发生变化,用户登录,我希望保持该 cookie 保持不变,并在应用程序初始化时自动加载。

我如何在这个框架中做到这一点?

更新另一个问答
供参考:http://developer.appcelerator.com/question/147601/alloy---persist-and-load-a-singleton-model#255723

【问题讨论】:

    标签: titanium titanium-mobile appcelerator-mobile


    【解决方案1】:

    他们在 appcelerator 文档中没有很好地解释它,但是如果你想使用内置的合金属性同步适配器存储和检索属性,你必须在使用模型时指定一个唯一的“id”。您已经在 xml 标记中做到了:&lt;Model id="userInfo" 但这仅适用于该视图文件。如果您想在控制器中访问/更新此属性,请执行以下操作:

    var UserInfo = Alloy.createModel("userInfo", {id: "userInfo"});
    UserInfo.fetch();
    UserInfo.set("cookie", "new value");
    UserInfo.save();
    

    如果你想在代码中保留对这个属性的引用,我相信你只需将它附加到alloy.js中的全局命名空间:

    var UserInfo = Alloy.createModel("userInfo", {id: "userInfo"});
    UserInfo.fetch();
    Alloy.Globals.UserInfo = UserInfo;
    

    在你做的控制器中:

    var UserInfo = Alloy.Globals.UserInfo;
    

    【讨论】:

      【解决方案2】:

      将你的模型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 中获取模型,只需将这个:&lt;Collection src="userInfo"/&gt; 放入您的 xml 文件中。

      作为旁注,我通常只使用Titanium.App.Properties 来存储用户信息。属性用于将与应用程序相关的数据存储在属性/值对中,这些数据在应用程序会话和设备电源循环之后仍然存在。例如:

      // 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);
      }
      

      【讨论】:

      • 谢谢你,我会记住Ti.App.Properties,合金模型有一个通过“属性”响应的适配器
      • 是的,但是我发现使用 Ti.App.properties 而不是适配器更容易,尤其是对于单例模型,如果只有一个,那么使用 sql 有什么意义。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多