简体   繁体   English

Ember statemanager,controller,view,model:如何将它们链接在一起?

[英]Ember statemanager, controller, view, model : how to link them together?

I'm facing a problem where I need to link state manager, controller and view together and at the same time avoid to get an ugly spaghetti code. 我正面临一个问题,我需要将状态管理器,控制器和视图链接在一起,同时避免得到丑陋的意大利面条代码。 And the question is : which of these object should be created first and has the responsibility to create the others ? 问题是:应该首先创建哪些对象并负责创建其他对象?

To be specific, here is my example. 具体来说,这是我的例子。 First the view is a subclass of a container view that has a collection view as child : 首先,视图是容器视图的子类,其集合视图为子视图:

App.MyView = Ember.ContainerView.extend {

     childViews: ['streamView']

     streamView: Ember.CollectionView.extend {
     }

}

The controller is just as subclass of Ember.ArrayController with a load method : 控制器就像Ember.ArrayController的子类一样,带有一个load方法:

App.MyController = Ember.ArrayController.extend {
     load: ->
        console.log "loading data"
}

The state manager has a view state which will instantiate App.MyView : 状态管理器有一个视图状态,它将实例化App.MyView:

App.MyStateManager = Ember.StateManager.extend {

   initialeState: 'ready'

   ready: Ember.ViewState.extend {

        view: App.MyView    

   }
}

Now I need to run the following test : 现在我需要运行以下测试:

 controller = App.MyController.create {}
 manager = App.MyStateManager.create {}

 expect(manager.getPath('currentState.name').toEqual('ready')
 expect(controller.load).toHaveBeenCalled()
 streamView = manager.getPath('currentState.view.streamView.content')
 expect(streamView.content).toEqual(controller.content)

In order to make the last expectation to work, I need to bind the content of my streamView that is a child of App.MyView with the controller content. 为了使最后的期望工作,我需要将我的作为App.MyView的子节点的streamView的内容与控制器内容绑定。 How can I do this cleanly ? 我怎么能干净利落地做到这一点?

Furthermore, how to pass a reference to the state manager to the view and the controller in order to notify the manager when some event occur so it need to transit to another state. 此外,如何将对状态管理器的引用传递给视图和控制器,以便在某些事件发生时通知管理器,因此需要转移到另一个状态。 For example a click on an item or controller did finish a job ? 例如,点击某个项目或控制器确实完成了一项工作?

Take a look at this gist by Yehuda Katz discussing the new router implementation. 看看Yehuda Katz讨论新路由器实现的这个要点。 https://gist.github.com/2728699 https://gist.github.com/2728699

The suggestion seems to be to make the router and by extension the state manager the point at which the 3 layers are tied together. 建议似乎是使路由器和扩展状态管理器将3层连接在一起的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM