当模型存储在数组中时,从一个模型绑定(bind)到另一个模型的“正确”方法是什么?通常我会想象这将是 Controller 的 content
数组,但为了保持示例简单:
MyApp.websites = [];
MyApp.websites.push(Ember.Object.create({
name: "Stackoverflow"
}));
MyApp.websites.push(Ember.Object.create({
name: "Serverfault"
}));
MyApp.favorite = Ember.Object.create({
// how should this be bound to a specific element of MyApp.websites?
nameBinding: ?????????
});
最佳答案
您可以使用一个属性来绑定(bind)它。
这样:
MyApp.websites = [];
MyApp.websites.push(Ember.Object.create({
name: "Stackoverflow"
}));
MyApp.websites.push(Ember.Object.create({
name: "Serverfault"
}));
MyApp.mainController = Ember.Object.create({
currentWebsiteIndex: 0,
currentWebsite: function() {
return MyApp.websites[this.get("currentWebsiteIndex")];
}.property("currentWebsiteIndex")
});
MyApp.favorite = Ember.Object.create({
// how should this be bound to a specific element of MyApp.websites?
nameBinding: "MyApp.mainController.currentWebsite.name"
});
这只是为了演示这个想法,更好的实现是:
MyApp.websites = Ember.ArrayProxy.create({
content: [],
currentWebsiteIndex: 0,
currentWebsite: function() {
return this.objectAt(this.get("currentWebsiteIndex"));
}.property("currentWebsiteIndex")
});
MyApp.websites.pushObject(Ember.Object.create({
name: "Stackoverflow"
}));
MyApp.websites.pushObject(Ember.Object.create({
name: "Serverfault"
}));
MyApp.favorite = Ember.Object.create({
// how should this be bound to a specific element of MyApp.websites?
nameBinding: "MyApp.websites.currentWebsite.name"
});
关于javascript - 存储在数组中的 Ember.js 绑定(bind)模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512568/