Ember.js绑定模型存储在数组中 [英] Ember.js binding models stored within an array

查看:127
本文介绍了Ember.js绑定模型存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当模型存储在数组中时,从一个模型绑定到另一个模型的正确方式是什么?通常我会想象这将是一个控制器的内容数组,但要保持简单的例子:

  MyApp.websites = []; 
MyApp.websites.push(Ember.Object.create({
name:Stackoverflow
}));

MyApp.websites.push(Ember.Object.create({
name:Serverfault
}));

MyApp.favorite = Ember.Object.create({
//如何绑定到MyApp.websites的特定元素?
nameBinding:???
});


解决方案

可以使用属性来绑定。 p>

这样:

  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({
//如何绑定到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({
//如何绑定到MyApp.websites的特定元素?
nameBinding:MyApp.websites。 currentWebsite.name
});


What is the 'correct' way to bind from one model to another when the models are stored within an array? Typically I'd imagine that this would be a Controller's content array, but to keep the example simple:

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: ?????????
});

解决方案

You can use a property to bind that.

This way:

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"
});

This is just to demonstrate the idea, a better implementation would be:

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"
});

这篇关于Ember.js绑定模型存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆