在非反应式环境中与uber/needle共享下游数据的最佳方式是什么?
让我提供一个简单的例子。我有一个评论列表,其中有一个相应的组件和ViewController。用户点击评论。将这个从评论列表ViewController中选择的评论传递回评论列表组件(父级),以便它可以引入一个新的依赖项,这是一个好方法。最终目标是comment detail组件(子组件)可以实例化所选评论的comment detail ViewController。
发布于 2021-01-27 15:16:24
根据示例,建议的方法是使用构建器作为评论列表VC的依赖项
final class CommentListComponent: Component<CommentListDependency>, CommentListBuilder {
private var commentDetailBuilder: CommentDetailBuilder {
commentDetailComponent(parent: self)
}
var commentListViewController: UIViewController {
CommentListViewController(commentDetailBuilder: self.commentDetailBuilder)
}
}
protocol CommentListDependency: Dependency {
}
protocol CommentListBuilder {
var commentListViewController: UIViewController
}正如您在上面看到的,评论列表vc将获得对构建器的引用,定义如下:
final class CommentDetailComponent: Component<CommentDetailDependency>, CommentDetailBuilder {
// CommentRef is either a model or an id to fetch it from a data store
func commentDetailViewController(commentRef: CommentRef) -> UIViewController {
CommentDetailViewController(commentRef: commentRef)
}
}
protocol CommentDetailDependency: Dependency {
}
protocol CommentDetailBuilder {
func commentDetailViewController(commentRef: CommentRef) -> UIViewController
}在评论列表vc中,一旦选择了评论,就可以调用commentDetailBuilder.commentDetailViewController(commentRef:)方法来传递对所选评论的引用,以获得一个详细的vc,然后您可以将其呈现出来。
您可以在他们的MVC示例中看到相同的模式,GameComponent将scoreSheetBuilder注入到GameViewController https://github.com/uber/needle/blob/5d8c0854b95ac9449228410d4282b625c3ceb6be/Sample/MVC/TicTacToe/Sources/Game/GameComponent.swift中
https://stackoverflow.com/questions/59950260
复制相似问题