angularjs - Making promises in modules -
i try make facebook registration module in app. facebook api faster angular controller, promise should used here. problem $q seems empty object , defer function undefined.
module:
var module = angular.module('app.facebook', []); module.constant("fbappid", 'herecomesmycode');
module.factory('facebook', facebookapi); facebookapi.$inject = ['$ionicloading', '$q', '$ionicplatform', '$state', 'authservice', 'datacontext', '$location']; function facebookapi(userservice, $q, $ionicloading, fbappid, $state, authservice, datacontext, $location) { return { fbloginsuccess: fbloginsuccess, fbloginerror: fbloginerror, getfacebookprofileinfo: getfacebookprofileinfo, fblogin: fblogin, fbregister: fbregister };
and here $q.defer undefined:
function fbregister() { console.log($q.defer); if (!cordova) { facebookconnectplugin.browserinit(fbappid); } var data; facebookconnectplugin.getloginstatus(function (response) { if (response.status !== 'connected') { facebookconnectplugin.login(["email"], function(response) { data = getapidata(); }, function(response) { }); } else { data = getapidata(); } }); }
without using promise, gets fast api variables want fill values api, initiated before api finishes , undefined.
the whole module:
(function() { 'use strict'; var module = angular.module('app.facebook', []); module.constant("fbappid", 'myappkey'); module.factory('facebook', facebookapi); facebookapi.$inject = ['$ionicloading', '$ionicplatform', '$state', 'authservice', '$q']; function facebookapi(userservice, $ionicloading, fbappid, $state, authservice, $q) { return { fbloginsuccess: fbloginsuccess, fbloginerror: fbloginerror, getfacebookprofileinfo: getfacebookprofileinfo, fblogin: fblogin, fbregister: fbregister } function fbregister() { console.log($q); if (!cordova) { facebookconnectplugin.browserinit(fbappid); } var data; facebookconnectplugin.getloginstatus(function (response) { if (response.status !== 'connected') { facebookconnectplugin.login(["email"], function(response) { data = getapidata(); }, function(response) { }); } else { data = getapidata(); } }); } function getapidata() { var formdata = {}; facebookconnectplugin.api("me/?fields=id,first_name,last_name,link,gender,email,birthday", ["public_profile", "email", "user_birthday"], function (result) { if (result.gender == "male") { result.gender = '1'; } else { result.gender = '2'; } formdata = { name: result.first_name + " " + result.last_name, email: result.email, birthday: new date(result.birthday), gender: result.gender } console.log("moduĊ" + formdata);//here have nice , neat data return formdata; }, function(res) { }); } }; //this success callback login method function fbloginsuccess(response) { var fblogged = $q.defer(); if (!response.authresponse) { fbloginerror("cannot find authresponse"); return; } var expdate = new date( new date().gettime() + response.authresponse.expiresin * 1000 ).toisostring(); var authdata = { id: string(response.authresponse.userid), access_token: response.authresponse.accesstoken, expiration_date: expdate } authservice.facebooklogin(response.authresponse.accesstoken).then(function() { fblogged.resolve(authdata); }); }; //this fail callback login method function fbloginerror(error) { var fblogged = $q.defer(); fblogged.reject(error); alert(error); $ionicloading.hide(); }; //this method user profile info facebook api function getfacebookprofileinfo() { var info = $q.defer(); facebookconnectplugin.api('/me', "", function(response) { info.resolve(response); }, function(response) { info.reject(response); } ); return info.promise; } //this method executed when user press "login facebook" button function fblogin() { if (!cordova) { //this browser facebookconnectplugin.browserinit(fbappid); } //check if have user's data stored var user = userservice.getuser(); facebookconnectplugin.getloginstatus(function(success) { //alert(json.stringify(success, null, 3)); if (success.status === 'connected') { // user logged in , has authenticated app, , response.authresponse supplies // user's id, valid access token, signed request, , time access token // , signed request each expire facebookconnectplugin.api("me/?fields=id,first_name,last_name,link,gender,email", ["public_profile", "email"], function(result) { //alert("result: " + json.stringify(result)); //alert(result.first_name); }) var accesstoken = success.authresponse.accesstoken; authservice.facebooklogin(accesstoken).then(function() { $state.go('app.map'); }, function(err) { alert('auth failed: ' + json.stringify(err, null, 2)); }); } else { //if (success.status === 'not_authorized') user logged in facebook, has not authenticated app //else person not logged facebook, we're not sure if logged app or not. $ionicloading.show({ template: 'loging in...' }); // permissions facebook facebookconnectplugin.login([ 'email', 'public_profile', 'user_about_me', 'user_likes', 'user_location', 'read_stream', 'user_photos' ], fbloginsuccess, fbloginerror); fblogged.promise.then(function(authdata) { var fb_uid = authdata.id, fb_access_token = authdata.access_token; //get user info fb getfacebookprofileinfo().then(function(data) { var user = data; user.picture = "http://graph.facebook.com/" + fb_uid + "/picture?type=large"; user.access_token = fb_access_token; //save user data //store on local storage should save on database userservice.setuser(user); $ionicloading.hide(); $state.go('app.map'); }); }); } }); }
})();
Comments
Post a Comment