嗨,我有这个 ember 模型。likeMessage 计算属性正在构建我在模板中使用的字符串。(见下文)这可行,但我不喜欢在我的模型中使用这个“视图层代码”。什么是更好的方法?
/**
* @class
* @name Entry
*/
James.Entry = Ember.Object.extend(
/** @lends Entry# */
{
likes: [],
likeMessage: function() {
var likes = this.get("likes"),
withNameCount = 0,
names = [],
likeCount = likes.length;
for(;withNameCount < likes.length && withNameCount < 2; withNameCount++) {
names.push(likes[withNameCount].name);
}
if(likeCount == 0) {
return "Nobody likes this";
} else if(likeCount == 1) {
return names[0]+ " likes this";
} else if(likeCount <= 2) {
return names.join(" and ")+" like this";
} else {
return names.join(", ")+" and "+(likes.length-2)+" others like this";
}
}.property("likes")
}
);
我的模板:
Likes:
{{likeMessage}}