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.
- import { Injectable } from '@angular/core';
- declare var ZOHO: any;
- @Injectable({
- providedIn: 'root'
- })
- /**
- * This is a service that encapsulates the zoho api for widgets. This service requires that the sdk js file is loaded
- * See https://www.zoho.com/creator/newhelp/app-settings/widgets/creator-api-for-widgets.html for more details.
- */
- export class ZohoInterfaceService {
- recordOps = ZOHO.CREATOR.API;
- IsInitialised: boolean = false;
- queryParams: any;
- constructor() {
- }
- /**
- * Initialises the zoho creator interface
- *
- * @returns A Promise containing the query parameters
- */
- Initialise(): Promise<any> {
- var p: Promise<any> = new Promise((resolve, reject) => {
- var creatorSdkPromise = ZOHO.CREATOR.init();
- creatorSdkPromise.then(this.InititPromise.bind(null, this, resolve)).catch(function (error) {
- console.log("Creator initialisation failed");
- console.log(error);
- reject(error)
- })
- })
- return p;
- }
- private InititPromise(service: ZohoInterfaceService, resolve, data) {
- service.queryParams = ZOHO.CREATOR.UTIL.getQueryParams();
- console.log("Creator Initialised");
- console.log(data);
- service.recordOps = ZOHO.CREATOR.API;
- service.IsInitialised = true;
- resolve(data);
- }
- /**
- *
- * @param uid - The unique id of the item that you are looking for
- * @param report - The report that you want to get the item from
- * @returns a promise containing the results of the search.
- */
- GetRecord(uid: string, report: string): Promise<any> {
- var config = {
- reportName: report,
- id: uid
- }
- console.log("Config :");
- console.log(config);
- return <Promise<any>>this.recordOps.getRecordById(config);
- }
- /**
- *
- * @param criteria - The criteria that needs to be applied to the search
- * @param report - The report that you want to search
- * @returns a promise containing the results of the search
- */
- GetAllRecords(criteria: string, report: string): Promise<any> {
- var config = {
- criteria: criteria,
- reportName: report
- }
- return this.recordOps.getAllRecords(config);
- }
- }