gpt4 book ai didi

javascript - Ember.js 适当引用不相关项目的属性

转载 作者:行者123 更新时间:2023-11-30 13:14:27 25 4
gpt4 key购买 nike

假设一个 ember.js 路由器应用程序具有一个应用程序 Controller 、应用程序 View 、一个即插即用的 View 类型,以及一个处理外部数据的无关 Controller 。第三个 View 如何从不相关的 Controller 中获得计算属性,我应该将什么放入 .property() 省略号以便它收到更改通知?

例如

App.ExternalDataController = Em.Controller.extend
stellarProperty: 'super value' #I want OtherView to have a computer property referencing this

App.ApplicationController = Em.ArrayController.extend
content: [] #Assume a bunch of values of some kind here

App.ApplicationView = Em.View.extend
templateName: 'app-view'

App.OtherView = Em.View.extend
templateName: 'other-view'
someComputedProperty: (->
App.router.externalDataController.get('stellarProperty') + ' flying pigs'
).property() #What do I put in that property elipses?

模板

<script type="text/x-handlebars" data-template-name="application">
<div>
{{#each content}}
{{name}} -- {{view App.OtherView}}
{{/each}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="other-view">
<span>Irate goats and {{view.someComputedProperty}}</span>
</script>

最佳答案

简短的回答是您应该通过 View 的 Controller 使您需要的属性可用。

在您的示例中,因为 OtherView 嵌套在 ApplicationView 的模板中,它会有一个 controller 属性设置为 应用程序 Controller 。在您的路由器中,您可以调用 router.applicationController.connectControllers('externalData')。这将在 applicationController 上设置一个 externalDataController 属性。然后你可以公开你需要的属性:

App.ApplicationController = Em.ArrayController.extend
content: [] #Assume a bunch of values of some kind here
externalDataController: null # set via connectControllers call in router
stellarPropertyBinding: Em.Binding.oneWay('externalDataController.stellarProperty')

OtherView 变成:

App.OtherView = Em.View.extend
templateName: 'other-view'
someComputedProperty: (->
@get('controller.stellarProperty') + ' flying pigs'
).property('controller.stellarProperty')

希望对您有所帮助!

关于javascript - Ember.js 适当引用不相关项目的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com