【发布时间】:2015-10-03 14:53:08
【问题描述】:
我在我的项目中使用 MVVM 模式实现 RAC,现在我开始怀疑了。 我对服务器有很多调用,但它们都与 UIButton 相关联并在我的 ViewModel 中处理;现在我需要在加载 UIViewController 时调用服务器。在 MVVM 之前,我刚刚在 viewDidLoad 方法中创建了一个信号 voilá!,但我不确定是否可以将它放入 ViewController。 现在我不知道如何将 RACSignal 绑定到 ViewController 中的事件,最糟糕的是,我不确定这是否是遵循 MVVM 模式的方式。
当我通过用户操作(来自 UIButton)调用服务器时,我现在正在做的是:
视图控制器*
self.someButton.rac_command = viewModel.executeSomeAction
//On success:
self.viewModel.executeLoginCompleted.skip(1).subscribeNextAs {
(isExecuting: Bool) -> () in
//Do something
}
//On error:
self.viewModel.executeSomeActionError.subscribeNextAs {
(error: NSError) -> () in
//Dd something
}
视图模型*
var executeSomeAction: RACCommand?
var executeSomeActionError: RACSignal!
var executeLoginCompleted: RACSignal
executeSomeAction = RACCommand(enabled: combineValidationSignals) {
(any:AnyObject!) -> RACSignal in
println("ANY: \(any)")
return self.executeLoginRequest()
}
executeSomeActionError = executeLogin!.errors
executeLoginCompleted = executeLogin!.executing
当 UIView 加载时,我应该如何创建 RACSignal 或 RACCommand?当然,遵循 MVVM 模式。
谢谢
【问题讨论】:
标签: ios swift mvvm binding reactive-cocoa