【问题标题】:Changing context/scope of Ember.ComputedProperty更改 Ember.ComputedProperty 的上下文/范围
【发布时间】:2016-06-05 18:33:56
【问题描述】:

正如标题所说,我希望找到一种方法来改变 Ember.ComputedProperty 的上下文或范围。

我的用例是我允许用户定义一个用于动态绑定的 bindingConfig 属性。此 bindingConfig 可以是字符串、函数或 Ember.ComputedProperty。

因为 bindingConfig 是与 Content/Model 分开传递的,因此我需要将它绑定到内容,但我还没有找到对 Ember.ComputedProperty 执行此操作的方法。

https://ember-twiddle.com/8c01af57f3a3b9fde858?openFiles=application.controller.js%2Capplication.template.hbs

非常感谢,:)

【问题讨论】:

  • 我不太确定你想做什么。 bindingConfig 返回什么以及您期望的行为是什么?
  • bindingConfig 可以是以下三种类型之一:“String”、“function”、“Ember.ComputedProperty”。我希望 get-prop 助手能够在每种情况下获得正确的属性。因此,如果它是一个字符串,只需执行“content.get(bindingConfig)”即可获取内容属性。但是,如果 bindingConfig 是 Ember.ComputedProperty,我希望它返回其函数内部的内容,但将上下文作为列表中的项目。我提供的链接包含一个更详细的示例。

标签: javascript ember.js binding scope computed-properties


【解决方案1】:

这是您的示例显示的内容:

{{my-component content=appObj bindingConfig="name"}}
{{my-component content=appObj bindingConfig=nickName}}

与此相同:

{{my-component content=appObj bindingConfig="name"}}
{{my-component content=appObj bindingConfig="Darth Meow"}}

考虑到appObj(或content)上没有Darth Meow 属性,ember 的行为符合预期。

你想要的是这个:

{{my-component content=appObj bindingConfig="name"}}
{{my-component content=this bindingConfig="nickName"}}

第二个组件现在将在您的controller 上查找nickName 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2021-09-27
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多