简体   繁体   English

灰烬数据-从子级获取父级值

[英]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, inverse: null }),
  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”属性,该属性将显示根窗口小部件的名称,或显示“父名称-子名称”的子窗口小部件

displayName: Ember.computed('name', 'parentWidget.name', 'isSubLob',  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 子lob的displayName总是以

undefined - WidgetName

The json is being returned like so: json像这样返回:

{
"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. 对于记录,所有记录都由json同时返回。

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. 我觉得Ember 应该异步解析父窗口小部件,并且字符串也应该更新,但是它似乎没有用。 Any idea what I'm doing wrong here? 知道我在做什么错吗?

I would say you have two issues: 我会说你有两个问题:

  1. You're not declaring an inverse to your parentWidget relationship, which means that Ember Data is guessing the inverse (and likely guessing wrong). 您并未在您的parentWidget关系中声明相反的parentWidget ,这意味着Ember Data在猜测相反的含义(并且可能会猜错)。 You should change that declaration to look like this, just to be sure: 您应该将声明更改为如下形式,以确保:

     parentWidget: DS.belongsTo('widget', { async: true, inverse: null }), 

    I doubt that will fix your issue, but it's good practice. 我怀疑这是否可以解决您的问题,但这是一个好习惯。

  2. You're not waiting for your promise to resolve before trying to use the name. 您不必等到您的承诺解决,再尝试使用该名称。 You've specified the parentWidget relationship as being asynchronous, which means that @get('parentWidget') will not return a model. 您已将parentWidget关系指定为异步关系,这意味着@get('parentWidget')不会返回模型。 It's going to return a promise that will eventually resolve to your model. 这将返回一个最终会解决您的模型的承诺 Normally this would be fine as the computed property would just recompute when the promise resolves, except that you're not watching the proper key. 正常情况下,这是可以的,因为在未解决承诺的情况下,compute属性只会重新计算,除非您没有注意正确的密钥。

     /* PS: Assuming that your comma was misplaced on this line */ displayName: Ember.computed('name', 'parentWidget', function() { ^^^^^^^^^^^^ 

    As seen, you're only watching the parentWidget property. 如图所示,您仅在观看parentWidget属性。 So if the name property on the parentWidget every updates, you won't be notified. 因此,如果parentWidget上的name属性每次更新,则不会通知您。 Change that line to this and you should be good to go: 将该行更改为此,您应该很好:

     displayName: Ember.computed('name', 'parentWidget.name', function() { 

    Just keep in mind that the first few times through, parentWidget.name will still be undefined . 请记住,前几次, parentWidget.name仍然是undefined It won't be the value you want until the promise resolves, which means the computed property could run several times before it does resolve. 直到promise解析后,它才是您想要的值,这意味着calculated属性在解析之前可以运行多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM