Alt 0.3.1

Alt 0.3.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2015年11月
SPM支持 SPM

Francis Chong 维护。



Alt 0.3.1

  • 作者: francis chong
  • siuying

Alt

Swift 的另一个 Flux 实现。它利用 Swift 语言提供了“单向数据流”和类型安全的模块概念。

使用方法

步骤 1:定义动作

  • 创建一个实现 Action 协议的 struct。
struct TodoActions {
    struct Create : Action {
        let title : String
    }
}

步骤 2:定义存储和注册动作

  • 创建一个 Store 类,其中实现 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()
    }
}

步骤 3:在视图中监听存储事件

  • 使用 Store.listen() 注册对 store.state 变更的监听
self.todoStore.listen { (state) -> (Void) in
    self.tableView.reloadData()
}

步骤 4:创建和分发动作

TodoActions.Create(title: "New ToDo").dispatch()

要求

  • Swift 2.0

安装

Alt 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "Alt"

作者

Francis Chong, [email protected]

许可证

Alt 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。