Ember StateManager,注入控制器并使用initialState

时间:2012-06-07 10:35:45

标签: ember.js

我有以下StateManager来处理几个视图。我希望不必手动转换到App.stateManager.transitionTo('showingPhotos')的初始状态。我更喜欢使用状态管理器的initialState属性。

在这种情况下,使用StateManager的initialState属性不起作用,因为控制器在注入App.initialize(App.stateManager)之前不可用。

有没有办法避免在注入控制器的同时手动转换到初始状态?是否有更好的方法来构建像这样的州经理?

我创建了两个JSfiddles:

http://jsfiddle.net/GmD8A/ - 这有效但我必须手动转换到初始状态

http://jsfiddle.net/tgbuX/ - 这使用initialState而不是手动转换到初始状态,因此不起作用。

PhotosListView = Ember.View.extend({
  template: Ember.Handlebars.compile('<h2>Showing Photos</h2><a {{action "showContacts"}}>Show Contacts</a>')
});

ContactsListView = Ember.View.extend({
  template: Ember.Handlebars.compile('<h2>Showing Contacts</h2><a {{action "showPhotos"}}>Show Photos</a>')
});

StateManager = Ember.StateManager.extend({
  rootElement: '#body',
  showingContacts: Ember.ViewState.extend({
    view: ContactsListView,
    showPhotos: function(manager) {
      manager.transitionTo('showingPhotos');
    },
    enter: function(manager) {
      this._super(manager);
      this.setPath('view.controller', manager.get('photosController'));
    }
  }),
  showingPhotos: Ember.ViewState.extend({
    view: PhotosListView,
    showContacts: function(manager) {
      manager.transitionTo('showingContacts');
    },
    enter: function(manager) {
      this._super(manager);
      this.setPath('view.controller', manager.get('contactsController'));
    }
  })
});

App                    = Ember.Application.create()
App.PhotosController   = Ember.ArrayController.extend()
App.ContactsController = Ember.ArrayController.extend()
App.stateManager       = StateManager.create()

App.initialize(App.stateManager) // This injects the controllers
App.stateManager.transitionTo('showingPhotos') // I don't want to have to manually transition into this initial state

1 个答案:

答案 0 :(得分:2)

您应该查看最新的Ember版本,该版本现在有一个路由器,可以与任何*Controller类实例一起自动装配,并以您希望的方式管理outlets

你可以从https://gist.github.com/2679013&amp; https://gist.github.com/2728699

修改

您有一个新的进展指南@ https://emberjs-staging-new.herokuapp.com/guides/outlets#toc_the-router