0

I want to expose my models to one component and show in one table. I want this to be modular in a way that I can use this component to other models.

The way I have done the attributes which have certain relationship do not show up. The problem that I found out is because by the time I grab it, the promise has not been resolved and I am not grabbing the attributes using {{ember-data}}. I cant figure out one way of doing that... I'm really new to ember and its' been a problem for me...

UPDATE

Before reading all the description below, the thing is that if I can convert the model to one array it would be enough, I guess. So, I could do something like this:

{#each model as |item|}}
  <tr>
  {{#each item as |column index|}}
    <td>{{column.[index]}} &nbsp;</td>
  {{/each}}
  </tr>
{{/each}}

END OF UPDATE

I have the following two models

// #models/inventory-summary.js
export default DS.Model.extend({
    name: DS.attr('string'),
    total: DS.attr('number'),
    projects: DS.hasMany('inventoryProject'), //I WANT TO SHOW THIS GUY HERE
});
// #models/inventory-project.js
export default DS.Model.extend({
    name: DS.attr('string'), // IN PARTICULAR THIS ONE
    summary: DS.hasMany('inventorySummary'),
});

Templates:

// #templates/inventory-summary.js
// here I'm sending the model to the component and mapping the attribute to something readable
{{model-table model=inventorySearchResult columnMap=columnMap}}

// #templates/components/model-table.hbs
// here is where I show the value of the model
{{#each model_table.values as |item|}}
   {{getItemAt item index}}
{{/each}}

My helper

export function getItemAt(params/*, hash*/) {
    return params[0][params[1]];
}

And in my route I'm doing:

// #routes/inventory-summary.js
model(params) {
    let query_params = {page_size: 100};
    if (params.page !== undefined && params.page !== null) {
        query_params['page'] = params.page;
    }
    return Ember.RSVP.hash({
        inventoryProject: this.get('store').findAll('inventoryProject'),
        inventorySummary: this.get('store').query('inventorySummary', query_params),
    });
},
setupController(controller, models) {
    this._super(...arguments);
    controller.set('projects', models.inventoryProject);
    controller.set('inventorySearchResult', models.inventorySummary);
    let columnMap = [
        ['name', 'Name ID',],
        ['total', 'Total'],
        ['projects', 'Project', {'property': 'name', 'has_many': true}]
    ];
    controller.set('columnMap', columnMap);
},

Finally this is the part of the code which is really messed up which is where I'm passing to the template the values I'm trying to show

// #components/model-table.js
getValueForColumn(values, params) {
    if (values.length > 0) {
        if (typeof values[0] === "object") {
            return this.getResolved(values, params);
        } else {
            return values;
        }
    }
    return values;
},
getResolved(promise, params) {
    let result = [];
    promise.forEach( (data) => {
        data.then((resolved) => {
            let tmp = "";
            resolved.forEach((item) => {
                console.log(item.get(property)); // THIS GUY SHOWS UP LATER
                tmp += item.get(property) + ", ";
            });
            result.push(tmp);
        });
    });
    return result; // THIS GUY RETURN AS EMPTY!
},
didReceiveAttrs() {
    this._super(...arguments);
    let model = this.get('model');
    let columnMap = this.get('columnMap');
    for (var i = 0; i < columnMap.length; i++) {
        attributes.push(columnMap[i][0]);
        columns.push({'name': columnMap[i][1], 'checked': true});
        values.push(this.getValueForColumn(model.getEach(columnMap[i][0]), columnMap[i][2])); //WRONG HERE
    }
    this.set('model_table', {});
    let model_table = this.get('model_table');
    Ember.set(model_table, 'values', values);
 },

I am able to show in the template if I start doing a bunch of if's in the {{template}}, because I believe the template does some kind of binding I am not doing and it resolves later, but it was really ugly and nasty. I wanted to do something cleaner... that's why I'm posting here.

renno
  • 2,659
  • 2
  • 27
  • 58

1 Answers1

0

move your api calls to afterModel. and try something like this where you wait for promise to resolve and set it.

 afterModel: function () {
 var rsvp = Ember.RSVP.hash({
  obj1:    Ember.$.getJSON("/api/obj1"),
  obj2: Ember.$.getJSON("/api/obj2"),
});
var ret = rsvp.then(function (resolve) {
  console.log("Something" + resolve.obj1);
  self.set('obj2', resolve.obj2);

});
return ret;

},

setupController: function (controller, model) {
this._super(controller, model);
controller.set('obj1', this.get('obj1'));
controller.set('obj2, this.get('obj2'));

}

Sandeep
  • 585
  • 7
  • 19
  • Seems better than using Ember.observer(). I will take a look on that and give a return soon... – renno Sep 30 '16 at 16:20
  • Actually this does not work for me because there is no afterModel() for components which is what I'm using. I'm passing the result in the template at `model=inventorySearchResult` – renno Oct 04 '16 at 15:47
  • You can do the same in model() also i guess. – Sandeep Oct 07 '16 at 03:43