Backbone collection.get()不会使用is方法恢复模型

时间:2013-05-22 14:23:58

标签: javascript backbone.js collections model

我正在开发一个在线/离线应用程序,我注意到了这一点:

我在我的收藏中从localStorage获取我的离线检索。 (每次用户在线登录时,我都会使用从我的服务器检索到的附加数据来保存他)

如果同一个用户现在尝试离线登录,我会检查此运算符是否与我的集合中的任何运算符匹配,如果它与条目匹配,则返回运算符匹配。

问题是,我从collection.get(#id)获得的运算符没有骨干运算符模型中的任何方法。

在纠正我的代码之前看起来像这样:

if( this.isLocalValidOperator( operator ) ){
  var operatorMatch = this.get(operator.get('id'));
  cb( operatorMatch );
}

我的更正,现在可行了

if( this.isLocalValidOperator( operator ) ){
  var operatorMatch = this.get(operator.get('id'));
  // must add additional attributes from the match, then return the operator created with new Operator( someAttributes )
  operator.set({ 
    isSuperadmin: operatorMatch.get('isSuperadmin'),
    isModerator: operatorMatch.get('isModerator'),
    firstname: operatorMatch.get('firstname'),
    lastname: operatorMatch.get('lastname')
  });
  cb( operator );
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果有其他人遇到这个或类似的丢失方法问题......我在从localStorage恢复时丢失方法,但属性数据仍然存在。问题是在集合中我错过了属性:

model: ModelName

我加入后,方法在页面重新加载时保持不变。