Ember 没有获得某些属性

2023-12-12

当从以下位置运行以下命令时UserController在 Google Chrome 上,ember-couchdb-kit-0.9, 余烬数据v1.0.0-beta.3-56-g8367aa5, Ember v1.0.0, and 这个 couchdb 适配器:

customerSignUp: function () {
            var model = this.get('model');
            var customer = this.get('store').createRecord('customer', {
                description: 'Why hello sir',
                user: model
            });
            customer.save().then(function() {
                model.set('customer', customer);
                model.save();
            });
        }

使用这些模型:

App.User = App.Person.extend({
    name: DS.attr('string'),
    customer: DS.belongsTo('customer', {async: true })

App.Customer = DS.Model.extend({
    user: DS.belongsTo('user', {async: true}),
    description: DS.attr('string')
});

用户和客户都没有正确设置他们的关系(在 Ember 调试器中,用户有null并且客户有<computed>,而不是某种<EmberPromiseObject>这就是他们在工作时所拥有的)。 仅当有问题的对象被持久化时才会发生这种情况。如果save()调用被省略,两者都已正确设置关系,但当然数据库尚未使用此信息进行更新。每当保存发生时,关系就会被空条目覆盖。

我发现问题出在适配器上serializeBelongsTo函数,我现在已将其副本更改为以下内容:

serializeBelongsTo: function(record, json, relationship) {
      console.log("serializeBelongsTo");
      console.log(record.get('user'));
      console.log(json);
      console.log(relationship);
      var attribute, belongsTo, key;
      attribute = relationship.options.attribute || "id";
      console.log(attribute);
      key = relationship.key;
      console.log(key);
      belongsTo = Ember.get(record, key);
      console.log(belongsTo);
      if (Ember.isNone(belongsTo)) {
        return;
      }
      json[key] = Ember.get(belongsTo, attribute);
      console.log(Ember.get(belongsTo, attribute));
      console.log(json);
      if (relationship.options.polymorphic) {
        return json[key + "_type"] = belongsTo.constructor.typeKey;
      }
      else {
        return json;
      }
    }

attribute, belongsTo, and key all log as correct, but console.log(Ember.get(belongsTo, attribute)); returns undefined, which I've tried to change to console.log(Ember.get(Ember.get(belongsTo, 'content'), attribute)); since console.log(belongsTo); told me the id attribute was hidden inside a content object. Attached is a screenshot showing what I mean. screenshot from 2013-12-07 18 15 52 The change doesn't fix the problem though, and I keep getting undefined. No matter what method I use to try to get the id out of the belongsTo object, I always get either null or undefined. Here are some examples of things I've tried to get content out of the object:

var content = belongsTo.content;
var content = Ember.get(belongsTo, 'content');
var content = belongsTo.get('content');

console.log(json);回报Object {description: "Why hello sir", user: undefined}

这是一个显示相关输出的pastebin:http://pastebin.com/v4mb3PJ2

Update

一个非常混乱的更新!

当我从不同的函数保存模型时:

saveModel: function() {
    this.get('model').save().then(
        function( data, textStatus, jqXHR ) {
            console.log('Saved successfully.');
        },
        function( jqXHR, textStatus, errorThrown ) {
            console.log(jqXHR);
            console.log(errorThrown);
            console.log(textStatus);
        }
    );
}

模型已正确保存。一切都在serializeBelongsto完全按照预期工作。

这是一个不同的 Pastebin,显示了这种情况的输出:http://pastebin.com/Vawur8Q0


我解决了这个问题。基本上是belongsTo对象在serializeBelongsTo当它被引用时并没有真正解决,我通过查询发现了这一点isFulfilled。所以我通过这种方式保存侧面来实现:

function saveOn (target, attribute) {
    target.addObserver(attribute, function () {
        if (target.get(attribute)) {
            console.log("Inside with %@".fmt(attribute));
            target.removeObserver(attribute);
            Ember.run.once(target, function() {
                target.save();
            });
        }
    });
};

customerSignUp: function () {
    var model = this.get('model');
    var customer = this.get('store').createRecord('customer', {
        description: 'Why hello sir'
    });
    customer.save().then(function () {
        model.set('customer', customer);
        customer.set('user', model);
        saveOn(customer, 'user.isFulfilled');
        saveOn(model, 'customer.isFulfilled');
    });
}

现在一切都像魅力一样。这可能是个好主意serializeBelongsTo不过要考虑到这一点。这行:console.log(Ember.get(belongsTo, 'isFulfilled'));即将到来false就我而言。在记录的创建和序列化之间只是存在某种竞争条件!

我想让我的saveOn函数返回一个承诺,然后我可以用它来链接多个saveOn在一起。这样我就不必做customer.save()以确保 id 已填充。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ember 没有获得某些属性 的相关文章

随机推荐