我有一个名为User
的backbonejs模型。用户具有城市、州和国家属性。我在这个模型上有一个名为locationString的函数,它根据模型上的属性是否预先设置,输出一个逗号分隔的列表。例如,亚特兰大,佐治亚州或佐治亚州或亚特兰大,所以如果需要的话,只有逗号。
我通过调用var json = model.toJSON()
在视图的render方法中设置模板的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;
},
发布于 2013-04-12 05:45:23
我发现这是因为我调用的是this.field_name
,而不是this.get('field_name')
。
我希望这对其他人有帮助。
https://stackoverflow.com/questions/15957591
复制相似问题