Skip to content Skip to sidebar Skip to footer

Ember Get Not Getting Certain Attribute

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, Ember v1.0.0, and this couchdb adapter: custo

Solution 1:

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:

functionsaveOn (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.

Post a Comment for "Ember Get Not Getting Certain Attribute"