javascript - 存储在数组中的 Ember.js 绑定(bind)模型

标签 javascript ember.js

当模型存储在数组中时,从一个模型绑定(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/

相关文章:

javascript - Iframe 滚动溢出隐藏在 css 中不起作用

javascript - 给定提取的组合文本字符串中的偏移量,如何确定包含节点和偏移量?

javascript - 如何在单击图像时在同一页面上的多个图像上使用引导模式?

javascript - webgl readpixels 总是返回 0,0,0,0

Ember.js:无法访问注入(inject)的对象

coffeescript - Ember.JS && Coffeescript

javascript - *ngIf 内的 viewChild 产生 undefined reference

ruby-on-rails - 如何使用 Ember Data & Rails 在单个 Ember.js 表单中保持 hasMany 关联?

使用 promises 测试 ember 组件

javascript - Ember "transition was aborted"