Ember get not getting certain attribute

前端 未结 1 2008
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 14:43

When running the following from the UserController on Google Chrome, with ember-couchdb-kit-0.9, Ember Data v1.0.0-beta.3-56-g8367aa5,

1条回答
  •  无人及你
    2021-01-20 15:43

    I figured out the problem. Basically the belongsTo object in serializeBelongsTo wasn't really resolved by the time it was being referenced, which I found out by querying isFulfilled. So I implemented by saving side this way:

    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');
        });
    }
    

    Now everything works like a charm. It might be a good idea for serializeBelongsTo to take this into account though. This line: console.log(Ember.get(belongsTo, 'isFulfilled')); was coming up false in my case. There was just a race condition of some sort between the creation of the record and it's serialization!

    I'd like to make my saveOn function return a promise though, which I could then use to chain multiple saveOns together. That way I wouldn't have to do a customer.save() to make sure the id's were populated.

    0 讨论(0)
提交回复
热议问题