1

アプリケーションコントローラー、アプリケーションビュー、プラグインして操作するビュータイプ、および外部データを処理する無関係のコントローラーを備えたember.jsルーターアプリケーションを想定しています。その3番目のビューは、無関係のコントローラーから計算されたプロパティをどのように.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>
4

1 に答える 1

3

簡単に言うと、必要なプロパティをビューのコントローラーから利用できるようにする必要があります。

この例では、のテンプレートにOtherViewネストされているため、に設定されたプロパティがあります。ルーターでは、を呼び出すことができます。これにより、にプロパティが設定されます。次に、必要なプロパティを公開できます。ApplicationViewcontrollerApplicationControllerrouter.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 に答える