GithubHelp home page GithubHelp logo

nami-client's Introduction

NAMI小程序前端JS库

NAMI容器配套客户端SDK。NAMI资料:https://github.com/wodenwang/nami

How to use?

1.引用nami文件

var nami = require("./nami/index"); 

2.修改/nami/config.js中的host地址,修改为NAMI服务端地址

3.登录请求

nami.login(() => {
    // success callback
},() => {
    // fail callback
});

4.获取用户信息请求

nami.getUserInfo(res => {
    // success callback
},res => {
    // fail callback
});

5.网络请求

nami.request({
    url:"", // 请求地址
    data:{},// 请求数据
    loading:0, //0代表请求时title不显示loading(默认),1代表请求时title显示loading
    success:function(res) {
      // success callback
    },
    fail:function(res) {
       // fail callback
    }
});

6.nami支持promise异步请求,封装原有的login(),getUserInfo(),request()请求,我们可以通过链式操作处理回调

  • 登录请求
nami.promise.login().then(() => {
    // success callback
}).catch(() => {
    // fail callback        
});
  • 获取用户信息请求
nami.promise.getUserInfo().then(res => {
    // success callback
}).catch(res => {
    // fail callback        
});
  • 网络请求
nami.request({
    url:"", // 请求地址
    data:{},// 请求数据
    loading:0 //0代表请求时title不显示loading(默认),1代表请求时title显示loading
}).then(() => {
      // success callback
  }).catch(() => {
      // fail callback        
  });

promise在开发中的应用,可参考该demo:

实际开发中会遇到这种情况,如果先用nami.login()实现登录以获取NAMI_TOKEN;再通过nami.getUserInfo()获取用户数据,在调试工具中发现namiToken为空(异步请求导致的错误,还没有获取NAMI_TOKEN,就发送getUserInfo请求),返回报错,如图: 异步请求错误


** 看看用promise怎么处理这类问题 **

  • 核心代码,如图 promiseCode

  • 看看数据请求:

1.登录请求 promiseCode

2.获取用户数据 promiseCode

3.网络请求 promiseCode

nami-client's People

Contributors

chou1213 avatar wodenwang avatar

Watchers

James Cloos 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.