0

我开始使用 ember 并尝试从 Api 获取一些数据并将其分配给 Emberjs 模型。

我的 api 返回如下内容:

    [
    {
        email: 'someemail@domain.com'
        name: {
            firstname: 'first name',
            lastname: 'last name'
        }
    }
    {
        email: 'someemail2@domain.com'
        name: {
            firstname: 'another first name',
            lastname: 'another last name'
        }
    },
    {
        email: 'someemail3@domain.com'
        name: undefined
    }

]

我的代码是:

App.Store = DS.Store.extend({
  revision: 12,
});

App.Store.registerAdapter('App.Users', DS.Adapter.extend({
  findAll: function(store, type, id) {
    $.getJSON('https://domain.com/users?callback=?', {
        'auth_token': token
    }).done(function(json){
        store.loadMany(type, json);
    });
  },
}));

App.Store.registerAdapter('App.Users', DS.Adapter.extend({
  findAll: function(store, type, id) {
    $.getJSON('https://domain.com/users?callback=?', {
        'auth_token': token
    }).done(function(json){
        store.loadMany(type, json);
    });
  },
}));

App.Router.map(function() {
    this.route("users", { path: "/users" });
});

App.UsersRoute = Ember.Route.extend({
  model: function() {
    return App.Users.find();
  }
});

App.Users = DS.Model.extend({
    email: DS.attr('string'),
    firstname: DS.attr('string'),
    didLoad: function() {
        console.log('model loaded', this.toJSON());
    }
});


 <script type="text/x-handlebars" data-template-name="users">
    {{#each model}}
        {{email}} - {{firstname}}
    {{/each}}
</script>

显然 firstname 对于数组中的每个对象都是空的。

有谁知道这样做的正确方法是什么?

谢谢

4

1 回答 1

1

您是否尝试过像这样设置您的用户模型:

App.Adapter.map('App.User', {
  名称:{嵌入:'总是'}
});

App.User = DS.Model.extend({
  电子邮件:DS.attr('string'),
  名称:DS.belongsTo('App.UserName')
});

App.UserName = DS.Model.extend({
  名字:DS.attr('string'),
  姓氏:DS.attr('string')
});
于 2013-04-07T08:50:58.930 回答