0

我可能会做出承诺,但在验证用户身份后,我想将用户的个人资料加载到App.Session我创建的单例中:

App.Session.set(
    'userProfile', 
    self.get('store').find('user',App.Session.get('userId'))
);

这会导致进行 API 调用并返回有效的结果集,但由于某种原因在调试器中我得到一个空结果。具体来说,我确实看到了User.id但其余的列是空白的。

来自调试器的 JSON 响应如下:

{
  "user": {
    "id": "1",
    "username": "jsmith",
    "name": {
      "first_name": "Joe",
      "last_name": "Smith"
    },
    "emails": [
      {
        "id": "52153c0330063",
        "name": "work-1",
        "type": "other",
        "address": "new@notreally.com",
        "comments": "",
        "status": "active",
        "is_primary": false
      },
      {
        "id": "52153d1b90ad0",
        "name": "work-2",
        "type": "other",
        "address": "old@yesreally.com",
        "comments": "",
        "status": "active",
        "is_primary": true
      },
  ]
}

我对 Promises 有点陌生,所以我想如果我将代码更改为:

self.get('store').find('user',App.Session.get('userId')).then( function(profile){
    App.Session.set('userProfile', profile);
});

在编写新代码时,我对自己的新 Promise 敏锐度感觉非常好。可悲的是,我的骄傲时刻以失败告终。我的第二个代码片段的行为与第一个完全相同。嗯?

任何人都可以帮忙吗?

- - - - - ** 更新 ** - - - - -

我现在已经包含了模型定义User和我引用的调试器窗口的图片。

用户模型

App.RawTransform = DS.Transform.extend({
    deserialize: function(serialized) {
        return serialized;
    },  
    serialize: function(deserialized) {
        return deserialized;
    }
});
App.NameTransform = DS.Transform.extend({
    deserialize: function(serialized) {
        return App.Name.create(serialized);
    },
    serialize: function(deserialized) {
        return JSON.stringify(deserialized);
    }
});

App.User = DS.Model.extend({
    username: DS.attr("string"),
    name: DS.attr("name"),
    roles: DS.attr("raw"),
    goals: DS.attr("raw"),
    places: DS.attr("raw"),
    emails: DS.attr("raw"),
    networks: DS.attr("raw"),
    relationships: DS.attr("raw"),
    services: DS.attr("raw"),
    uom: DS.attr("raw"),
});

调试窗口

在登录之前,模型查看器如下所示: 前

然后登录后是这样的:

后

然后查看我们看到的记录详细信息:

细节

4

1 回答 1

1

好吧,答案似乎归结为两件事。首先,用于处理我尝试过的承诺的第二个代码片段:

self.get('store').find('user',App.Session.get('userId')).then( function(profile){
    App.Session.set('userProfile', profile);
});

正确的的方法。第一种方法只是让你在“破碎的心”中留下一个“破碎的承诺”,从技术上讲不是这样,但关键是它不起作用。

我的第二个 promise 实现不起作用的原因是Model间接且非常具体地归因于我为Names.

由于反序列化器已经在 Ember-Data v0.1x 世界中工作,所以我为此挠头,所以我做了看起来合适的事情……我责怪 Ember-Data。来吧,我们都做到了。事实是 Ember-Data 与它无关,一旦我愿意接受责备,我意识到这只是没有将我的Name对象转移到我目前正在进行的项目中的问题。嗬!

于 2013-10-02T18:48:11.883 回答