jeudi 29 septembre 2016

Ember display model which has relationship in one component

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 . I cant figure out one way of doing that... I'm really new to ember and its' been a problem for me...

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


// #templates/components/model-table.hbs
// here is where I show the value of the model

   


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 , 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.




Aucun commentaire:

Enregistrer un commentaire