新手的错误? JSON和Ember

时间:2014-03-27 12:30:36

标签: ember.js

这里有新的东西,非常努力学习 - 并了解我出错的地方。

我正在喂json,看起来像这样:

"status": "success",
  "data": {
    "groups": [
      {
        "id": 2,
        "name": "Test1234",
        "kind": "Happy",
        "parent_group_id": 1,
        "children_count": {
         "boy": 10,
         "girl": 4,
         "pets": 2
        },
        "is_top_level": true
      },

获取此信息的路径为/ api / groups / top。

我有一个(我相信会是)简单的app.js:

App = Ember.Application.create();

App.Router.map(function() {             
  this.route("login", {path: "/"});     
  this.route("groups");
    this.resource('about');
 });

App.GroupsRoute = Ember.Route.extend({
model: function() {
     return App.Group.find('top');
}

});

App.LoginController = Ember.Controller.extend({
  actions: {
login: function(username, password){
        jQuery.ajax({
           url: "/api/auth",
            type: 'post',
                data: JSON.stringify( { "username": "user", "password": "pass" } ),
            contentType: 'application/json',
            dataType: 'json',
        async: false,
        success: function(result) {
            if (result.status === 'success') {
                    user = result.data.user;
                    App.User.id = user.id;
                    App.User.name = user.name;

                    App.User.isAuthenticated = true;
                    App.User.displayUnits = "F";
                                } else {
                    //debugger;
                    throw "The! username and/or password you have entered is incorrect, please try again ";
                    return false;
                }
            },
            error: function(xhr, status, errorOut) {
                throw "The? username and/or password you have entered is incorrect, please try   again ";
            return false;
            }
        });
        if (App.User.isAuthenticated)
            this.transitionToRoute('groups');
    }}
});

App.RESTAdapter = RL.RESTAdapter.create({
  host: 'http://localhost:3000',
  namespace: 'api/'
});

App.Client = RL.Client.create({
  adapter: App.RESTAdapter
});

App.User = RL.Model.extend({
    username: RL.attr('string'),
      id: RL.attr('string'),
      isAuthenticated: RL.attr('boolean'),
    });

App.Group = RL.Model.extend({
    id: RL.attr('string'),
    name: RL.attr('string'),
    kind: RL.attr('string')
    });

App.RESTAdapter = RL.RESTAdapter.create({
  defaultData: { cookie: "data"}
});

我试图展示每个"组"在我的模板中。例如{{#each groups}} {{name}} {{/ each}},但没有运气 - 我可以在检查器中看到响应中的JSON数据,并且没有错误 - 但仍然没有任何结果。

任何帮助?

谢谢!

Ptep

编辑 - 模板:

 <script type="text/x-handlebars">

        {{outlet}}
      </script>

 <script type="text/x-handlebars" id="groups">
            <h3>Hello, 
              {{#each groups}}

              {{name}}! <br>
              {{kind}}, {{id}}
             {{/each}}
                        </h3>

</script>

1 个答案:

答案 0 :(得分:0)

数组本身就是模板的上下文,因此您可以使用

{{#each}}{{name}}{{/each}}

{{#each model}}{{name}}{{/each}} 

{{#each controller}}{{name}}{{/each}}

{{#each item in controller}}{{item.name}}{{/each}}