0

Ember.js で $.getJSON を使用しようとしています (基本的に、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 に答える