Hello.js参考3

杂项

分页功能,限制和下一页

做为所有结果的子集,响应必然会提供一个response.paging.next属性。这个属性能够塞回hello.api,以取得结果的下一页。javascript

在下面的示例中,函数paginationExample()调用了hello.api("me/frields")。后续的调用从resp.paging.next中取得path。html

javascriptfunction paginationExample(path) {
    hello('facebook')
        .api(path, {limit: 1})
        .then(
            function callback(resp) {
                if (resp.paging && resp.paging.next) {
                    if (confirm('Got friend ' + resp.data[0].name + '. Get another?')) {
                        // Call the API again but with the 'resp.paging.next` path
                        paginationExample(resp.paging.next);
                    }
                }
                else {
                    alert('Got friend ' + resp.data[0].name);
                }
            },
            function() {
                alert('Whoops!');
            }
        );
}

paginationExample('me/friends');

scope

scope属性定义了应用须要从网络提供者那边取得哪些特权。经过hello.init(object,{scope:'string'}),scope能够被定义为会话全局性的,而hello('network').login({scope:'string'});只在触发身份验证时可用。java

每一个应用能够指定多个scope,能够用逗号隔开,以下所示:node

javascripthello('facebook').login({scope: 'friends,photos,publish'});

scope与API请求是紧密耦合的,当会话缺失,或者不可用时,它将断开。git

下表描述了HelloJS曝露的默认scope。能够添加额外的scope以增长专有服务。可是必须当心不要混用专用scope和你不知道如何使用的其余服务。github

scope description
default 读取基本我的信息
"friends" 读取朋友的我的信息
"photos" 读取用户相册以及图片
"files" 读取用户文件
"publish" 发布状态更新
"publish_files" 发布图片和文件

把使用限制在一个scope里是个良好的做法,并且还让用户意识到为何你的应用程序须要某些特权。当用户须要用你的应用的更多功能时,再尝试更新受权。举个例子,若是用户想把一个连接分享给朋友,包括一个用户必须点击的按钮,以触发scope为"feiends"的hello.login,而后在验证身份以后处理器触发API调用。数据库

错误处理

hello.api([path]).then(null,[errorHandler])可以返回错误信息。或者使用hello.api([path],[handlesuccessOrError])windows

Promise响应标准化了error处理器的绑定。api

错误对象

一个失败请求的errorHandler对象的第一个参数,多是boolean(false),也多是一个Error Object。promise

Error Object对象的结构:{code:string,message:string}。

  • code:Code,required
  • message:Error message,required

扩展服务

添加到HelloJS的服务是模块化的。欲知更多关于建立你本身的模块的信息和示例,请看Modules

OAuth代理

|方法|网站
|OAUTH2:隐式受权服务,不须要服务器端受权|windows、google、facebook、Instagram、soundcloud、foursquare|
|OAUTH2:显式受权服务,服务必需oauth_proxy|windows、google、facebook、Instagram、linkedin、soundcloud、foursquare、github|
3.OAUTH1 & 1A:服务必需oauth_proxy|dropbox、twitter、yahoo、fickr|

对于只支持OAuth1或者OAuth2显式受权的服务提供者,身份验证流必须用一个密钥才能登入,这个密钥可能不会曝露在浏览器中。HelloJS用oauth_proxy定义了一个中间媒介,经过这个Web服务,HelloJS规避了这个问题。必须向这项服务提供一个anaccess_token,这项服务从数据库查找密钥并执行握手。若是是用OAuth1,网站服务器还登入了后续的API请求。

快速起步:在OAuth代理服务器上注册你的ClientID以及密钥。

这个默认的代理服务是https://auth-server.herokuapp.com/。开发员能够把他们本身ClientID以及密钥添加到服务器上,从而启动并运行。

另外一种办法是用node-oauth-shim从新建立这个服务,而后在HelloJS客户端脚本hello.init中中覆盖默认的oauth_proxy,以下所示:

javascripthello.init(
    CLIENT_IDS,
    {
        oauth_proxy: 'https://auth-server.herokuapp.com/proxy'
    }
)

Promises A+

异步方法hello.loginhello.logout以及hello.api的响应返回了一个可延迟的方法,就是Promise A+兼容。

若是你把库绑定在“src/*”文件里,请看这个promises-demo以了解更多。

浏览器支持

HelloJS瞄准全部的现代浏览器。

填充物包含在“scr/hello.polyfill.js”中,它提供了旧浏览器兼容功能。若是你使用了放在“dist/”目录下所源码,它已经绑定好了。可是若是你是从源码开始构建的,你可能喜欢先定义是否必需要用于填充物,或者你已经支持了它们。

PhoneGap支持

HelloJS同时还能够支持在PhoneGap应用上。请看这个hellojs-phonegap-demo以了解更多。

Hello.then

函数hello.apihello.login以及hello.logout返回一个Promise/A+ 1.1.1兼容的then方法。

示例:Hello.api().then()

这个示例演示了如何在调用hello.loginhello.api时使用Promise对象以操做响应。

javascriptfunction login(network){
    // Make a login call and handle the response using promises
    hello.login(network).then(function(){
        console.log('fullfilled', 'making api call');
        // Reurn another promise to get the users profile.
        return  hello( network ).api( 'me' );
    }).then(function(p){
        // Print it to console.
        console.log('hello.api(me) was fullfilled', p);
        return p;
    }).then(function(p){
        // Insert it into thumbnail
        document.getElementById(network).innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to "+ network+" as " + p.name;
    }).then(null, function(e){
        // Uh oh
        console.error(e);
    });
}

示例:Promise.all

得到google好友以及联系人

javascriptfunction friendsAndContacts(network){
    // Make a login call and handle the response using promises
    hello(network).login({scope:'friends'}).then(function(){
        console.log('fullfilled', 'making api call');
        // Reurn another promise to get the users profile.
        return Promise.all([
            hello( network ).api( 'me/friends' ),
            hello( network ).api( 'me/contacts' )
        ]);
    }).then(function(all){

        // Build a list of friends
        var a = [];
        for(var i=0;i<all.length;i++){
            for(var j=0;j<all[i].data.length;j++){
                a.push(all[i].data[j].name);
            }
        }
        document.getElementById('friends').innerHTML = a.join(',');

    }, function(e){
        // Uh oh
        console.error(e);
    });
}

初始化应用

javascripthello.init( {
    google : CLIENT_IDS.google
},
{
    redirect_uri : '../redirect.html'
});

hellojs.phonegap

相关文章
相关标签/搜索