Javascript 从Ember视图获取模型属性
我使用FireBase的模型设置了路线:Javascript 从Ember视图获取模型属性,javascript,ember.js,ember-data,emberfire,Javascript,Ember.js,Ember Data,Emberfire,我使用FireBase的模型设置了路线: App.ApplicationRoute = Ember.Route.extend({ model: function() { return EmberFire.Array.create({ ref: new Firebase("https://some-database.firebaseio.com/shape") }); }, setupController: function(controller
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return EmberFire.Array.create({
ref: new Firebase("https://some-database.firebaseio.com/shape")
});
},
setupController: function(controller, model) {
controller.set('model', model);
}
});
在模板中,我将显示视图中的每个阵列对象,每个:
当前视图中显示了一个颜色列表,但当我尝试访问属于此对象的模型属性时,我得到了“未定义”。是否需要执行其他设置?您可以使用
contentBinding
{#each controller}}
{{#view App.ColorView contentBinding=this}}
<div>{{color}}</div>
{{/view}}
{{/each}}
{#每个控制器}
{{{#view App.ColorView contentBinding=this}
{{color}}
{{/view}
{{/每个}}
谢谢您的回答。绑定内容并调用var item=this.getProperties('color');控制台日志(项目);我收到了对象{color:undefined}。您需要在视图中使用this.get('content.color')
。我认为这个.getProperties('color')试图从您的视图中检索,但您的视图没有颜色,只有内容。啊,明白了。非常感谢!当我设置一个新的颜色时,我的视图会改变,但是模型不会持续,你知道更新属性的正确方法吗?如果你使用contentBinding=color
,当颜色改变时,视图会被更新。
App.ColorView = Ember.View.extend({
click: function() {
var theColor = this.get('content');
console.log(theColor);
}
});
{#each controller}}
{{#view App.ColorView contentBinding=this}}
<div>{{color}}</div>
{{/view}}
{{/each}}