2016-09-29 76 views
0

我想将我的模型展示给一个组件并在一个表中显示。我希望这是模块化的,我可以将这个组件用于其他模型。在一个组件中有关系的灰烬显示模型

我已经完成了具有一定关系的属性的方式没有出现。我发现的问题是因为当我抓住它的时候,诺言还没有解决,我没有使用{{ember-data}}来获取属性。我不能想出这样做的一种方式......我真正的新余烬和其一直是我的一个问题...

UPDATE

阅读下面的所有描述之前,东西如果我可以将模型转换为一个数组,那我想这就足够了。所以,我可以做这样的事情:

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

结束时更新的

我有以下两种模式

// #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/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}} 

我的助手

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

而在我的路线我做:

// #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); 
}, 

最后这是真的搞砸代码的一部分就是我传递到模板我想的价值观显示

// #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); 
}, 

我能在模板中显示,如果我开始做一堆的,如果公司在{{模板}},因为我相信此范本某种结合我没有做,它解决了后来,但它真的很丑,很讨厌。我想做一些更干净的东西...这就是为什么我在这里发布。

回答

0

将您的api调用移至afterModel。 和 尝试类似这样的地方,等待承诺解决并设置它。

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

}

+0

似乎比使用Ember.observer更好()。我会看看这个,并很快回复... – mk2

+0

其实这对我来说不起作用,因为我没有使用组件的afterModel()。我在'model = inventorySearchResult'处将模板结果传递给模板' – mk2

+0

您可以在model()中做同样的事情,我也猜测。 – Sandeep