【问题标题】:Dynamic Attributes on Backbone model骨干模型上的动态属性
【发布时间】:2013-04-11 19:49:03
【问题描述】:

我有一个名为User 的backbonejs 模型。用户具有城市、州和国家属性。我在这个模型上有一个名为 locationString 的函数,它根据模型上的属性是否为 presnet 输出一个逗号分隔的列表。即亚特兰大,乔治亚州或乔治亚州或亚特兰大,因此只有在需要时才使用逗号。

我通过调用var json = model.toJSON() 在视图的渲染方法中为我的模板设置json,然后通过执行json.location_string = this.model.locationString(); 设置位置字符串。

我还通过在视图的初始化函数中调用this.model.bind('change', this.render); 将模型的更改事件绑定到我的渲染方法。

视图中的模型更新时所有其他属性都已相应更改,但 location_string 属性没有。

任何帮助将不胜感激

location_string: function() {                                                                                                                                                                                      var loc_str = "";      
loc_str += this.city || "";      
loc_str += (this.city && this.geo_state) ? ", " : "";      
loc_str += this.geo_state || "";      
loc_str += (this.geo_state && this.country) ? ", " : "";      
loc_str += this.country || "";      
return loc_str;    
},

【问题讨论】:

  • 你确定json.location_string = this.model.locationString(); 第二次调用你的render 时会产生不同的值吗?
  • 好问题,我刚刚检查过,它返回的是同样的东西。我在用户模型的 _.bindAll 中有 locationString 函数。这里有什么我应该与众不同的地方吗?
  • 嗯,如果locationString() 每次都返回相同的内容,您应该发布该函数,因为问题可能就在那里。
  • 我在帖子中添加了位置字符串函数。

标签: backbone.js backbone-views backbone-model


【解决方案1】:

我发现这是因为我打电话给this.field_name,而不是this.get('field_name')

我希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 2013-07-23
    • 2012-12-02
    • 1970-01-01
    • 2013-10-14
    • 2013-11-19
    相关资源
    最近更新 更多