1

Assuming an ember.js router application with an application controller, application view, a view type that I plug and play with, and an unrelated controller that handles external data. How can that third view have a computed property from the unrelated controller, what do I put into the .property() elipses so that it gets notified of changes?

e.g.

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?

templates

<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>
4

1 回答 1

3

简短的回答是您应该通过视图的控制器使您需要的属性可用。

在您的示例中,因为OtherView嵌套在ApplicationView的模板中,所以它将具有controller设置为 的属性ApplicationController。在您的路由器中,您可以调用router.applicationController.connectControllers('externalData'). 这将externalDataControllerapplicationController. 然后你可以公开你需要的属性:

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

希望有帮助!

于 2012-09-27T02:39:01.373 回答