jeudi 21 mai 2015

Ember Data - Get parent value from child

I have a widget model which has a shallow parent-child relationship. A given widget may be a "root" widget and not have any parent, or it may be a child widget which has a parent.

The ember data model looks like this:

export default DS.Model.extend({
  name:         DS.attr('string'),
  parentWidget: DS.belongsTo('widget', { async: true }),
  isSubWidget:  DS.attr('boolean')
})

I'm trying to add a "displayName" property that will show the name for root widgets, or "parent name - child name" for child widgets

displayName: Ember.computed('name', 'parentWidget,' function() {
  if this.get('isSubWidget') {
    return "#{this.get('parentWidget.name')} - #{@get('name')}"
  }
  else {
    return "#{this.get('name')}"
  }
})

This is not working, however. The child lob's displayName always comes as

undefined - WidgetName

The json is being returned like so:

{
"widgets": [
  {
    "id": 2,
    "name": "Widget Name",
    "is_sub_widget": true,
    "parent_widget_id": 1
  },
  ...
}

For the record, all the records are being returne by the json at the same time.

I feel like Ember should be asyncronously resolving the parent widget and the string should be updated as well, however it doesn't seem to be working. Any idea what I'm doing wrong here?




Aucun commentaire:

Enregistrer un commentaire