Ember.js控制器不工作 [英] Ember.js Controller Does not work
问题描述
我无法访问我的数组控制器变量。我有这个简单的应用程序例如:
App.js
App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.Router.map(function(){
this.route('hello');
});
App.IndexRoute = Ember.Route.extend({
redirect:function(){
this.transitionTo('hello');
}
});
App.HelloController = Ember.ArrayController.extend({
name:'tom'
});
App.HelloRoute = Ember.Route.extend({
model:function(){
return this.store.find('hello');
}
});
App.Hello = DS.Model.extend({
title:DS.attr('string')
});
App.Hello.FIXTURES = [{
id:1,
title:'hello'
},{
id:2,
title:'hello'
}];
我的意见是:
< script type =text / x-handlebarsdata-template-name =application>
< h2>欢迎使用Ember.js< / h2>
{{outlet}}
< / script>
< script type =text / x-handlebarsdata-template-name =hello>
{{#each}}
{{title}} {{name}}
{{/ each}}
< / script>
当我保存此代码的页面只是渲染hellohello hello tom。我做错了什么?
帮助感谢!
问题是你要将每个语句的上下文改为模型,所以你不再有名字上下文。
http:// emberjs。 jsbin.com/UzUxIwa/1/edit
< script type =text / x-handlebarsdata -template-name =hello>
{{#each item in controller}}
{{item.title}} {{controller.name}}
{{/ each}}
< / script>
老实说比这更好的是使用一个itemController, >
http://emberjs.jsbin.com/UzUxIwa / 2 / edit
App.HelloController = Ember.ArrayController.extend({
itemController:' singleHello'
});
App.SingleHelloController = Ember.ObjectController.extend({
name:'tom'
});
{{#each item in controller}}
{{item.title}} {{item.name}}< / br>
{{/ each}}
I can't access to my array controller variables. I have this simple application for example:
App.js
App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.Router.map(function() {
this.route('hello');
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('hello');
}
});
App.HelloController = Ember.ArrayController.extend({
name: 'tom'
});
App.HelloRoute = Ember.Route.extend({
model: function() {
return this.store.find('hello');
}
});
App.Hello = DS.Model.extend({
title: DS.attr('string')
});
App.Hello.FIXTURES = [{
id: 1,
title: 'hello'
},{
id: 2,
title: 'hello'
}];
and my views are:
<script type="text/x-handlebars" data-template-name="application">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="hello">
{{#each}}
{{title}} {{name}}
{{/each}}
</script>
when i save this code the page just render "hello" "hello" and i expect "hello tom" "hello tom". What i'm doing wrong?
Help is appreciated!
The problem is you're changing the context in your each statement to the model, so you no longer have the name in context.
http://emberjs.jsbin.com/UzUxIwa/1/edit
<script type="text/x-handlebars" data-template-name="hello">
{{#each item in controller}}
{{item.title}} {{controller.name}}
{{/each}}
</script>
Honestly even better than this would be to use an itemController and put the property on that controller
http://emberjs.jsbin.com/UzUxIwa/2/edit
App.HelloController = Ember.ArrayController.extend({
itemController:'singleHello'
});
App.SingleHelloController = Ember.ObjectController.extend({
name: 'tom'
});
{{#each item in controller}}
{{item.title}} {{item.name}}</br>
{{/each}}
这篇关于Ember.js控制器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!