我正在尝试将$ .getJSON与Ember.js一起使用(基本上我试图避免使用Ember-Data)。这是我的代码,
App = Ember.Application.Create();
App.Model = Ember.Object.extend({
});
App.Users = App.Model.extend({
id: null,
name: null
});
App.UsersRoute = Ember.Route.extend({
model: function(){
return App.Users.findAll();
}
});
App.Users.reopenClass({
findAll: function() {
var result = Ember.ArrayProxy.create({content: []});
$.getJSON('user.php', function(data) {
$.each(data, function(i, row) {
result.pushObject(App.Users.create(row));
});
});
return result;
}
});
这是我的HTML:
<body>
<script type="text/x-handlebars" data-template-name="MyTemplate">
{{#each item in controller }}
<tr><td>
<p> {{item.name}}</p>
</td></tr>
{{/each}}
</script>
<script type="text/x-handlebars">
<h1>Application Template</h1>
{{outlet}}
</script>
</body>
我遇到的问题是不是加载模型,我是否也需要控制器?或者我缺少的任何其他东西?
答案 0 :(得分:2)
您在创建应用程序时只会遇到一个小错误。
App = Ember.Application.create();
PS:你的代码看起来很好。只丢失了路由器映射,但我猜你故意将这个映射离开了你的例子。
<强>更新强>
您应该为UsersRoute
:
App.Router.map(function() {
this.resource("users", { path: "/users" });
});
您的模板应相应地命名为users
:
<script type="text/x-handlebars" data-template-name="users">
{{#each item in controller }}
<tr><td>
<p> {{item.name}}</p>
</td></tr>
{{/each}}
</script>
最后,您应该在主应用程序模板中创建指向Route的链接:
<script type="text/x-handlebars">
<h1>Application Template</h1>
{{outlet}}
{{#linkTo "users"}} Link to UsersRoute{{/linkTo}}
</script>