2016-04-28 25 views
3

我可以更改此代碼以便返回承諾嗎?谷歌日曆API調用可以在角廠內返回一個承諾嗎?

var calApi = { 
fun3: function() { 
    gapi.auth.authorize(
     { 
     'client_id': CLIENT_ID, 
     'scope': SCOPES.join(' '), 
     'immediate': true 
     }, calApi.fun2); 
}, 
fun2: function(authResult) { 
    if (authResult && !authResult.error) { 
     calApi.fun4(); 
    } 
}, 
fun1: function(event) { 
    gapi.auth.authorize(
     {client_id: CLIENT_ID, scope: SCOPES, immediate: false}, 
     calApi.fun2); 
    return false; 
}, 
fun4: function() { 
    gapi.client.load('calendar', 'v3', calApi.fun5); 
}, 
fun5: function() { 
    // some code 
    // returning the result... 
} 
}; 
return calApi; 

我的控制器:

$scope.hanleAPICall = function(event) { 
    factoryName.fun1(event); 
}; 

這個代碼是從谷歌calendar api採取和IM努力實現它在angularjs工廠,但我需要從API的返回事件的結果承諾儘快回覆事件,但在我的頁面內沒有任何事情發生。

+0

'gapi.client.load(...)'和其他收益的承諾? –

+0

我認爲他們都沒有返回承諾,如您可以在[鏈接](https://developers.google.com/google-apps/calendar/quickstart/js#step_2_set_up_the_sample)中看到的 – Dimitar

+0

如果返回值不是承諾您始終可以使用角度$ q模塊將其變爲承諾。 –

回答

1

你可以做類似下面返回服務承諾:

angular.module('app', []) 
.factory('AppService', function ($q) { 
    return { 
    fun3: fun3 
    }; 

    function fun3() { 
    var defer = $q.defer(); 

    gapi.auth.authorize(
     { 
     'client_id': CLIENT_ID, 
     'scope': SCOPES.join(' '), 
     'immediate': true 
     }, handleAuthResult); 

    function handleAuthResult (authResult) { 
     if (authResult && !authResult.error) { 
     defer.resolve(authResult); 
     } else { 
     defer.reject(authResult.error); 
     } 
    } 

    return defer.promise; 
    } 
}); 

與上述相類似,你可以爲你的其他方法工廠方法fun1, fun2, fun4, fun5

1

您可以Promisify一切, 什麼是你需要(在我看來)是一個圍繞gapi庫的包裝,就是這樣。

現在:

  1. 避免全局變量,創建兩個常量(GAPI_CLIENT_ID,GAPI_SCOPES)
  2. 創建[Service][1]是做什麼gapi的確但在一個角的方式。

這應該是你的代碼的一個小重構......

function GapiServiceFactory($q, GAPI_CLIENT_ID, GAPI_SCOPES) { 
 
    var self = this; 
 
    
 
    /** 
 
    * @private 
 
    **/ 
 
    self._authorize = function(immediate) { 
 
    var deferred = $q.defer(); 
 
    var data = { 
 
     client_id: GAPI_CLIENT_ID, 
 
     scope: GAPI_SCOPES, 
 
     immediate: !!immediate 
 
    }; 
 
    
 
    gapi.auth.authorize(data, function(result) { 
 
     if(Object.hasOwnProperty.call(result, 'error')) { 
 
     return deferred.reject(result); 
 
     } 
 
     
 
     return deferred.resolve(result); 
 
    }); 
 
    
 
    return deferred.promise; 
 
    }; 
 
    
 
    self.authorize = function() { 
 
    return self._authorize(false); 
 
    }; 
 
    
 
    self.authorizeImmediate = function() { 
 
    return self._authorize(true); 
 
    }; 
 
    
 
    self.loadCalendar = function() { 
 
    var deferred = $q.defer(); 
 
    
 
    gapi.client.load('calendar', 'v3', function() { 
 
     
 
     if(/**What's the logic here? **/) { 
 
     return deferred.reject(); 
 
     } 
 
     
 
     return deferred.resolve(); 
 
    }); 
 

 
    return deferred.promise; 
 
    }; 
 
} 
 

 
angular 
 
    .module('Test', []) 
 
    .constant('GAPI_CLIENT_ID', 12333) 
 
    .constant('GAPI_SCOPES', ['read', 'write']) 
 
    .service('GapiService', GapiServiceFactory) 
 
    .controller('TestCtrl', function TestCtrl(GapiService) { 
 
    GapiService 
 
     .authorizeImmediate() 
 
     .then(
 
     console.log.bind(console, 'Authorization success'), 
 
     console.log.bind(console, 'Authorization error') 
 
    ) 
 
    ; 
 
    
 
    GapiService 
 
     .loadCalendar() 
 
     .then(
 
     console.log.bind(console, 'LoadCalendar success'), 
 
     console.log.bind(console, 'LoadCalendar error') 
 
    ) 
 
    ;  
 
    }) 
 
;