Git Product home page Git Product logo

fanracmvvm's Introduction

FanRACMVVM

*MVVM的设计模式(RAC的使用)

简单的登录实现

//ViewController实现
@weakify(self);
RAC(self.viewModel,userName)=self.userNameTextField.rac_textSignal;
RAC(self.viewModel,password)=self.passwordTextField.rac_textSignal;
self.regestButton.rac_command=self.viewModel.regesterCommand;
self.loginButton.rac_command=self.viewModel.loginCommand;
[self rac_liftSelector:@selector(toggleHUD:) withSignals:RACObserve(self.viewModel, executing), nil];
[self rac_liftSelector:@selector(showMessage:) withSignals:[[RACObserve(self.viewModel, error) ignore:nil] map:^id (id value) {
    return [value localizedDescription];
}], nil];

[[RACObserve(self.viewModel, modelDic) ignore:nil] subscribeNext:^(id x) {
    @strongify(self);
    [self showMessage:@"登录成功"];
    //跳转到
    NSLog(@"登录结果:%@",x);
    SBJson4Writer *jsmodel4=[[SBJson4Writer alloc]init];
    NSString *msg=[jsmodel4 stringWithObject:x];
    self.showTextView.text=msg;
}];

//ViewModel实现
-(RACCommand *)loginCommand{
    if (!_loginCommand) {
        @weakify(self);
        _loginCommand=[[RACCommand alloc]initWithEnabled:
                       [RACSignal combineLatest:@[RACObserve(self, userName), RACObserve(self, password)] reduce:^id (NSString *userName, NSString *password){
            //1.字符串校验
            return @(([FanClass validateUserName:userName]&&[FanClass validatePassword:password]));
        }]signalBlock:^RACSignal *(id input) {
            //2.网络请求得到结果
            @strongify(self);
            return [self.apiManager loginWithUserName:self.userName password:self.password];
        }];
        
        [[_loginCommand.executionSignals concat] subscribeNext:^(id x) {
            //3.订阅结果
            @strongify(self);
            self.modelDic = x;
        }];
        [_loginCommand.errors subscribeNext:^(id x) {
            //4.订阅错误信息
            @strongify(self);
            self.error = x;
        }];
        [_loginCommand.executing subscribeNext:^(id x) {
            //订阅执行的状态
            @strongify(self);
            self.executing = x;
        }];
    }
    return _loginCommand;
}

###开发环境

  • OS X 10.11.2
  • Xcode Version 7.2

####有问题请直接在文章下面留言。 ####喜欢此系列文章可以点击上面右侧的 Star 哦,变成 Unstar 就可以了! ###开发人:凡向阳 ####Email:[email protected]

fanracmvvm's People

Contributors

fanxiangyang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.