我已经开始使用Ember,但是JSON数据并没有加载到视图中。此外,我不会在控制台上收到错误或警告。
这是我的app.js
App = Ember.Application.create({});
App.IndexRoute = Ember.Route.extend({
renderTemplate : function(controller) {
this.render('MyTemplate', {
controller : 'Index'
});
},
model : function() {
return App.MyTemplateModel.find();
}
});
App.IndexController = Ember.ArrayController.extend({
});
App.MyTemplateModel = Ember.Model.extend({
id : Ember.attr(),
last_name : Ember.attr(),
first_name : Ember.attr(),
suffix : Ember.attr(),
expiration : Ember.attr()
});
App.MyTemplateModel.url = "http://ankur1.local/index.php/api/example/users/";
App.MyTemplateModel.adapter = Ember.RESTAdapter.create();
var existing = App.MyTemplateModel.find();
App.MyTemplateModel.camelizeKeys = true;这是我的HTML
<body>
<script type="text/x-handlebars" data-template-name="myTemplate">
<input type="text" id="name" placeholder="name!"/>
<button {{action clickButton}} >Button</button>
{{view Ember.TextField valueBinding="userName"}}
<label >{{userName}}</label>
{{#each item in model}}
<tr><td>
{{id}} <small> {{item.first_name}}</small>
</td></tr>
{{/each}}
</script>
<script type="text/x-handlebars">
<h1>Application Template</h1>
{{outlet}}
</script>
</body>我的代码中可能遗漏了什么?
此外,我可以在控制台上使用
var u = App.MyTemplateModel.find(1);
u.get('first_name');发布于 2013-07-24 16:27:29
当您使用render或{{render}}助手时,您使用的是不同的控制器。你必须给它提供使用的模型。试着换到,
renderTemplate : function(controller) {
this.render('myTemplate', {
controller : controller
});
},这里,controller是由model钩子填充的IndexRoute ie:- IndexController的控制器。
编辑: Post jsbin
您正在使用MyTemplateModel循环视图中的项。您需要遍历content、model或controller,它们都对应于支持该路由的模型。
{{#each item in content }}
<tr><td>
{{item.id}} <p> {{item.first_name}}</p>
</td></tr>
{{/each}} 除此之外,您可能需要声明模型上的rootKey和collectionKey,以便正确识别json响应。
App.MyTemplateModel.rootKey = 'user';
App.MyTemplateModel.collectionKey = 'users';这是一个更新的吉斯宾。
发布于 2014-03-09 17:48:05
要使它工作,我需要做的是在我的JSON序列化程序中不指定根。换句话说,而不是..。
{
MyTemplateModel:
{
id: 1,
etc...
}
}看上去:
{
id: 1,
etc....
}我只能假设,由于成员模型已经期望模型MyTemplateModel作为一个响应,而它在JSON中不查找这个响应。希望这能有所帮助。
https://stackoverflow.com/questions/17832157
复制相似问题