Sample angular service for zoho widget api

Sample angular service for zoho widget api

After a week of frustration getting this to work (granted I'm still a newbie at web development) I thought that I should share the fruits of my development. Below you should have the basics of an Angular service written in Typescript that you'll be able to drop into just about any angular application.

I thought that I should pay it forward after all the help I got here for getting it to work in the first place.
  1. import {  Injectable } from '@angular/core';
  2. declare var ZOHO: any;

  3. @Injectable({
  4.   providedIn: 'root'
  5. })
  6.   /**
  7.    * This is a service that encapsulates the zoho api for widgets. This service requires that the sdk js file is loaded
  8.    * See https://www.zoho.com/creator/newhelp/app-settings/widgets/creator-api-for-widgets.html for more details.
  9.    */
  10. export class ZohoInterfaceService {

  11.   recordOps = ZOHO.CREATOR.API;
  12.   IsInitialised: boolean = false;
  13.   queryParams: any;
  14.   constructor() {
  15.   }
  16.   /**
  17.    * Initialises the zoho creator interface
  18.    *
  19.    * @returns A Promise containing the query parameters
  20.    */
  21.   Initialise(): Promise<any> {
  22.     var p: Promise<any> = new Promise((resolve, reject) => {
  23.       var creatorSdkPromise = ZOHO.CREATOR.init();
  24.       creatorSdkPromise.then(this.InititPromise.bind(null, this, resolve)).catch(function (error) {
  25.         console.log("Creator initialisation failed");
  26.         console.log(error);
  27.         reject(error)
  28.       })
  29.     })
  30.     return p;
  31.   }

  32.   private InititPromise(service: ZohoInterfaceService, resolve, data) {
  33.     service.queryParams = ZOHO.CREATOR.UTIL.getQueryParams();
  34.     console.log("Creator Initialised");
  35.     console.log(data);
  36.     service.recordOps = ZOHO.CREATOR.API;
  37.     service.IsInitialised = true;
  38.     resolve(data);
  39.   }
  40.   /**
  41.    *
  42.    * @param uid - The unique id of the item that you are looking for
  43.    * @param report - The report that you want to get the item from
  44.    * @returns a promise containing the results of the search.
  45.    */
  46.   GetRecord(uid: string, report: string): Promise<any> {
  47.     var config = {
  48.       reportName: report,
  49.       id: uid
  50.     }
  51.     console.log("Config :");
  52.     console.log(config);
  53.     return <Promise<any>>this.recordOps.getRecordById(config);
  54.   }

  55.   /**
  56.    *
  57.    * @param criteria - The criteria that needs to be applied to the search
  58.    * @param report - The report that you want to search
  59.    * @returns a promise containing the results of the search
  60.    */
  61.   GetAllRecords(criteria: string, report: string): Promise<any> {
  62.     var config = {
  63.       criteria: criteria,
  64.       reportName: report
  65.     }
  66.     return this.recordOps.getAllRecords(config);
  67.   }
  68. }