首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Uber Needle DI在非反应性环境中的使用

Uber Needle DI在非反应性环境中的使用
EN

Stack Overflow用户
提问于 2020-01-28 21:48:15
回答 1查看 104关注 0票数 0

在非反应式环境中与uber/needle共享下游数据的最佳方式是什么?

让我提供一个简单的例子。我有一个评论列表,其中有一个相应的组件和ViewController。用户点击评论。将这个从评论列表ViewController中选择的评论传递回评论列表组件(父级),以便它可以引入一个新的依赖项,这是一个好方法。最终目标是comment detail组件(子组件)可以实例化所选评论的comment detail ViewController。

EN

回答 1

Stack Overflow用户

发布于 2021-01-27 15:16:24

根据示例,建议的方法是使用构建器作为评论列表VC的依赖项

代码语言:javascript
复制
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将获得对构建器的引用,定义如下:

代码语言:javascript
复制
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示例中看到相同的模式,GameComponentscoreSheetBuilder注入到GameViewController https://github.com/uber/needle/blob/5d8c0854b95ac9449228410d4282b625c3ceb6be/Sample/MVC/TicTacToe/Sources/Game/GameComponent.swift

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59950260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档