当模型存储在数组中时,从一个模型绑定到另一个模型的“正确”方法是什么?通常我会想象这将是控制器的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: ?????????
});
您可以使用属性来绑定它。
这边走:
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"
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)