Swift 的另一个 Flux 实现。它利用 Swift 语言提供了“单向数据流”和类型安全的模块概念。
Action
协议的 struct。struct TodoActions {
struct Create : Action {
let title : String
}
}
Store
协议并带有 typealias State
Alt.getStore(YourStore.self)
获取存储的单例实例self.emitChange()
通知监听器关于 Store
中状态的变化class TodoStore : Store {
// define a typealias for the state of the model
typealias State = [Todo]
// and add a property of the State
var state : State!
// Set the initial state of the Store
static func getInitialState() -> State {
return []
}
required init() {
self.bindAction(TodoActions.Create.self, handler: self.onCreate)
// alternatively, bind action with a block
self.bindAction(TodoActions.Create.self) { [weak self] (action) -> () in
self?.state.append(Todo(title: action.title))
}
}
private func onCreate(action: TodoActions.Create) {
self.state.append(Todo(title: action.title))
self.emitChange()
}
}
Store.listen()
注册对 store.state
变更的监听self.todoStore.listen { (state) -> (Void) in
self.tableView.reloadData()
}
TodoActions.Create(title: "New ToDo").dispatch()
Alt 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "Alt"
Francis Chong, [email protected]
Alt 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。