如何在Controller中获取模型值 [英] How to get Model values in Controller
本文介绍了如何在Controller中获取模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试在控制器的操作
(alert)中获取点击的项目
的值,但是我总是得到未定义
。我想从模型中返回JSON(多个条目),并在控制器中访问它(不使用 FixtureAdapter
)。
< script type =text / x-handlebarsdata-template-name =first>
...
{{##each item in model}}
< p {{action'myClick'}}> {{item.name}}< / p>
{{/ each}}
...
App.FirstRoute = Ember.Route.extend({
model:function(){
返回[
{name:'Ruby'},
{name:'Java'}
]
//返回['Ruby','Java','Scala'] ;
}
});
App.FirstController = Ember.Controller.extend({
title:'FirstController'中的数据,
操作:{
myClick:function(){
alert('action in controller - >'+ this.get('model')。get('name'));
}
}
});
解决方案
为什么不把你的项目作为参数您的行动?
{{#每个项目在模型中}}
< p {{aciton'myClick'项目}}> {{item.name}}< / p>
{{/ each}}
App.FirstController = Ember.Controller.extend({
title:'FirstController',
actions :{
myClick:function(item){
alert('action in controller - >'+ item.name);
}
}
});
顺便说一句,你不返回 Ember.Object
在你的路线。您的模型是纯JavaScript对象的数组,而不是 item.get('name')
使用 item.name
/ p>
Trying to get the value of a clicked item
in controller's action
(alert) but I always get undefined
. I would like to return JSON (multiple entries) from model and access it in controller (without using FixtureAdapter
).
<script type="text/x-handlebars" data-template-name="first">
...
{{#each item in model}}
<p {{action 'myClick'}}>{{item.name}}</p>
{{/each}}
...
App.FirstRoute = Ember.Route.extend({
model: function(){
return [
{name: 'Ruby'},
{name: 'Java'}
]
//return [ 'Ruby', 'Java', 'Scala'];
}
});
App.FirstController = Ember.Controller.extend({
title:'data from FirstController',
actions: {
myClick: function() {
alert('action in controller -> ' + this.get('model').get('name'));
}
}
});
解决方案
Why don't you pass your item as parameter to your action?
{{#each item in model}}
<p {{aciton 'myClick' item}}> {{item.name}} </p>
{{/each}}
App.FirstController = Ember.Controller.extend({
title:'data from FirstController',
actions: {
myClick: function(item) {
alert('action in controller -> ' + item.name);
}
}
});
By the way you are not returning Ember.Object
in your route. Your model is array of pure javascript object so instead of item.get('name')
use item.name
这篇关于如何在Controller中获取模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文