javascript - Fast way to turn normal js into angular syntax? -
so have app hits gmail api, authorizes then, gets list of of email ids, fetches emails. have written in plain old java script want convert angular. i've watched bunch of videos , read lot of documentation find videos never go far doing this. how structure this? use factory or can drop js in controller method?
function handleclientload() { gapi.client.setapikey(apikey); window.settimeout(checkauth,1); } function checkauth() { gapi.auth.authorize({client_id: clientid, scope: scopes, immediate: true}, handleauthresult); } function handleauthresult(authresult) { var authorizebutton = document.getelementbyid('authorize-button'); if (authresult && !authresult.error) { authorizebutton.style.visibility = 'hidden'; makeapicall(); getmessages(); } else { authorizebutton.style.visibility = ''; authorizebutton.onclick = handleauthclick; } } function handleauthclick(event) { gapi.auth.authorize({client_id: clientid, scope: scopes, immediate: false}, handleauthresult); return false; } // load api , make api call. display results on screen. function makeapicall() { gapi.client.load('plus', 'v1', function() { var request = gapi.client.plus.people.get({ 'userid': 'me' }); console.log(request); request.execute(function(resp) { var heading = document.createelement('h4'); var image = document.createelement('img'); image.src = resp.image.url; heading.appendchild(image); heading.appendchild(document.createtextnode(resp.displayname)); document.getelementbyid('content').appendchild(heading); }); }); } }; function getmessages() { gapi.client.load('gmail', 'v1', function() { var request = gapi.client.gmail.users.messages.list({'userid' :'me', labelids: ['inbox'], }); request.execute(function(resp) { var content = document.getelementbyid("message-list"); angular.foreach(resp, function(value, key) { angular.foreach(value, function(value, key) { var newemail = gapi.client.gmail.users.messages.get({'userid': 'me','id': value.id ,'format':'metadata', 'metadataheaders': ['subject','from','to','date']}); newemail.execute(function(resp) { var emailw = resp; content.innerhtml += emailw.payload.headers[0].name + " "; content.innerhtml += emailw.payload.headers[0].value + "<br>"; content.innerhtml += emailw.payload.headers[1].name + " "; content.innerhtml += emailw.payload.headers[1].value + "<br>"; content.innerhtml += emailw.payload.headers[2].name + " "; content.innerhtml += emailw.payload.headers[2].value + "<br>"; content.innerhtml += emailw.payload.headers[3].name + " "; content.innerhtml += emailw.payload.headers[3].value + "<br><br><br><br><br><br><br><br>"; }); }); }); }); }); }
quickest way wrapping service/factory , exposing needed functions consumer. note don't have export functions, results in cleaner service.
angular.module('myapp').factory('mygapi', function() { // paste functions here function handleclientload() {} function makeapicall() {} // ... return { // expose function consumers makeapicall: makeapicall }; }); // consume mygapi service angular.module('myapp').controller('mycontroller', function($scope, mygapi) { $scope.click = function() { mygapi.makeapicall(); }; });
Comments
Post a Comment