0

我正在尝试将 $.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>

我遇到的问题不是加载模型,我也需要控制器吗?或者我还缺少什么?

4

1 回答 1

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>
于 2013-08-28T12:41:16.220 回答