为何无法通过需求获得应用程序控制器?

时间:2013-07-19 07:53:10

标签: ember.js

我在应用程序模板中实现了一个flash(消息)视图,它使用应用程序的msg属性来显示站点范围的消息:

<div class='application-main container'>
  {{#if msg}}
    {{view Yu.FlashView}}
  {{/if}}

  {{outlet}}
</div>

我的部分路由器:

Yu.Router.map ->
  @resource 'user', { path: '/users/:user_id' }

用户路线:

Yu.UserRoute = Em.Route.extend
  model: (params) ->
    Yu.User.find(params.user_id)

  setupController: (controller, model) ->
    @_super controller, model

    @controllerFor('avatar').set 'model', model.get('avatar')
    @controllerFor('basicinfo').set 'model', model.get('basicinfo')

  renderTemplate: (controller, model) ->
    @_super controller, model

    @render 'avatar', into: 'user', outlet: 'avatar'
    @render 'basicinfo', into: 'user', outlet: 'basicinfo'

正如您在进入用户路线时所看到的那样,我设置了basicinfo控制器并将其放入插座。 这是一个问题:我在basicinfo控制器中“需要”应用程序控制器,但我似乎无法工作:

Yu.BasicinfoController = Em.ObjectController.extend
  needs: 'application'.w()

  update: ->
    @content.save()
    @content.on('becameInvalid', ->
      window.console.log(Em.inspect(@get('controllers.application')))  !-> prints "undefined"
      @get('controllers.application').set 'msg', 'please refresh'      !-> msg did not show
    )
    @set 'inEditModel', false

我对需求api很困惑,这里有什么问题?谢谢!

1 个答案:

答案 0 :(得分:1)

我不太确定你的代码中存在什么问题,但这是一个简单的例子,证明它有效:http://jsfiddle.net/scottned/NQKvy/32/

<script type="text/x-handlebars" data-template-name="index">
  <ul>
    {{#each customer in customers}}
      <li>{{customer.firstName}} {{customer.lastName}}</li>
    {{/each}}
  </ul>
</script>

App = Ember.Application.create({});

App.ApplicationController = Em.ObjectController.extend({
    people: [
      {firstName: 'Kris', lastName: 'Selden'},
      {firstName: 'Luke', lastName: 'Melia'},
      {firstName: 'Formerly Alex', lastName: 'Matchneer'}
    ]
});

App.IndexController = Em.ObjectController.extend({
    content: Em.Object.create({}),
    needs: 'application',
    customersBinding: 'controllers.application.people'
});