다음 StateManager를 사용하여 몇 가지 뷰를 처리합니다. App.stateManager.transitionTo('showingPhotos')
을 사용하여 수동으로 초기 상태로 전환 할 필요가 없습니다. 상태 관리자의 initialState
속성을 사용하는 것을 선호합니다.Ember StateManager, 컨트롤러 주입 및 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