jeudi 3 mars 2016

Ember store doesn't automatically store the belongsTo data

I wasn't able to access the belongsTo relationship in store, the scenario is my current route is /sales-orders, so it display a grid of sales-orders, I have a link-to helper for each item, and when I click an item it should redirect to /sales-order/1,

/routes/sales-order
 model(params) {
    return this.store.findRecord('salesOrder', params.salesOrder_Id).then(function(salesOrder) {
    return salesOrder;
});

},

In my controller I have this init function that add and observer to my model

/controllers/sales-order
    init: function (){
        this._super.apply(this, arguments);
        this.addObserver('model', this, 'modelChange');
    },

    modelChange: function(sender, key, value, rev){

        if(this.get('openModalForEditList') != true ){
        this.set('paymentTerm', this.store.peekAll('sales-order-payment-term').objectAt(0).get('description'));
        this.set('shippingMethod', this.store.peekAll('sales-order-shipping-method').objectAt(0).get('shippingMethodName'));
        this.set('salesOrderDetails', this.store.peekAll('sales-order-detail'));
        //this.get('openModalForEditList') == true ? this.set('openModal', true) : this.set('openModal', false);
    }

The issue occurs here in modelChange function, while trying to access the this.store.peekAll('sales-order-payment-term').objectAt(0).get('description') or this.store.peekAll('sales-order-shipping-method').objectAt(0).get('shippingMethodName') the data from server doesn't initially store it to the ember store while the parent(sales-order) model is already loaded to the store. (It only load's the data after full transition I guess?)

So how to make the child/belongsTo data load together with parent to the ember store?

Below is my model definition.

//sales-order model (parent model)
export default DS.Model.extend({
  notes: DS.attr(),
  total: DS.attr(),
  salesOrderDetailRelationship: DS.hasMany('salesOrderDetail', {inverse: 'salesOrder'}),
  paymentTerm: DS.belongsTo('sales-order-payment-term'),
  shippingMethod: DS.belongsTo('sales-order-shipping-method')
});

//sales-order-payment-term
export default DS.Model.extend({  
description: DS.attr(),
salesOrder: DS.belongsTo('sales-order')});

//sales-order-shipping-method
export default DS.Model.extend({
    // shippingMethodId: DS.attr(),
    shippingMethodName: DS.attr(),  
    salesOrder: DS.belongsTo('sales-order')
});




Aucun commentaire:

Enregistrer un commentaire