余烬数据-在一个对象内部分配一个值

Ember Data - Assign a value inside an object

本文关键字:一个 分配 数据 一个对象 内部 余烬      更新时间:2023-09-26

我开始使用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对于数组中的每个对象都是空的。

有谁知道正确的方法是什么吗?

谢谢

您是否尝试过这样设置您的User模型:

<>以前App.Adapter.map("应用程序。用户的,{名称:{embedded: 'always'}});App.User = DS.Model.extend({电子邮件:DS.attr('字符串'),名称:DS.belongsTo("App.UserName")});App.UserName = DS.Model.extend({firstName: DS.attr('字符串'),姓:DS.attr(字符串)});