0

我在应用程序模板中实现了一个 flash(message) 视图,它使用应用程序的 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 很困惑,这里有什么问题?谢谢!

4

1 回答 1

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'
});
于 2013-07-19T16:54:46.160 回答