Source: common/services.js

/**
 * @module common/services
 * @description This module is responsible for general application service calls 
 */

define([

], 

function ()

{

	// ---------------------------------------------------------------
	//
	// SERVICES
	//
	// ---------------------------------------------------------------

	var Services = function()
	{   

        this.def = {
            PATH_JSON    : "content/json/",
            STATUS_OK    : "ok",
            STATUS_ERROR : "error"
        };

		this.jConfig = "";

        this.manifest = -1;
        
		this.init();
	};

	Services.prototype =
	{    
		// --------------------------------------------------------------
		// METHODS
		// --------------------------------------------------------------
		
		// ______________________________________________________________
		//                                                           init
        /**
            * init
            * @description Initializes class
            * @fires assignListeners()
            * @memberOf module:common/services
            * @instance
        */
		init: function()
		{      
			var self = this;

			console.log(" * <services>");
			
			if (typeof window.oModel === "undefined" || window.oModel.jConfig === -1){
                window.tEvent.addListener("EVENT_MODEL_READY", function(evt, data)
                {
                	self.jConfig = window.oModel.jConfig.services;
                    self.testMode = window.oModel.testMode;
                    self.assignListeners(); 
                    
                });                
            } else {
                self.jConfig = window.oModel.jConfig.services;
                self.testMode = window.oModel.testMode;
                self.assignListeners();  
            }

		},

		// ______________________________________________________________
		//                                                assignListeners
        /**
            * assignListeners
            * @description Assigns listeners to events used in this class.
            * @listens EVENT_TEST_MODE_STATE_CHANGE
            * @listens EVENT_ADMIN_GOT_MANIFEST
            * @memberOf module:common/services
            * @instance
        */
		assignListeners: function()
		{          
			var self = this;

            window.tEvent.addListener(window.tEvent.eventStr.EVENT_TEST_MODE_STATE_CHANGE, function(evt, data) {
                self.testMode = data;
            });

            // manifest load
            window.tEvent.addListener("EVENT_ADMIN_GOT_MANIFEST", function(evt, data){                   
                self.onGotManifest(data);
            });

		},


		// --------------------------------------------------------------
		// HELPERS
		// --------------------------------------------------------------
		
        // ______________________________________________________________
        //                                                   getReturnObj
        /**
            * getReturnObj
            * @description Returns generic return object
            * @returns {Object}
            * @example
            * {
            *    "status": "ok",
            *    "data": -1
            * }
            * @memberOf module:common/services
            * @instance
        */ 
        getReturnObj: function() {
            return({
                status: this.def.STATUS_OK,
                data: -1
            });
        },


		// ______________________________________________________________
        //                                                  sendAnalytics
        /**
            * sendAnalytics
            * @description Sends an analytic data using JSON services node configuration
            * @param {Object} formData - Analytic form data  
            * @memberOf module:common/services
            * @instance
        */
        sendAnalytics: function(formData) { 

            var config = this.jConfig.analyticsService;

            $.ajax({
                url: config.wsPath + formData,               
                type: "GET",
                timeout: 45000
          
            }).done(function(data) {
                console.log(" * <services.sendAnalytics> done", data);
            }).fail(function(data) {
                console.info(" * <services.sendAnalytics> fail", data);                
            });

        },


        // ______________________________________________________________
        //                                                loadProjectJson
        /**
            * loadProjectJson
            * @description Loads the project json that overrides the core-config.json
            * @param {String} filename - File name         
            * @param {Function} callback - Callback function  
            * @returns {callback(JSON)}     
            * @memberOf module:common/services
            * @instance
        */
        loadProjectJson: function(filename, callback) {
            var returnObj = this.getReturnObj();
            var bust = "?bust=" + Math.random();
            $.getJSON(this.def.PATH_JSON + filename + bust, function(projJson) {  
                console.log(" * <services.loadProjectJson>", projJson); 
                returnObj.data = projJson;
                callback(returnObj);
            });
        },


		// --------------------------------------------------------------
		// EVENTS
		// --------------------------------------------------------------

        // ______________________________________________________________
        //                                                  onGotManifest
        /**
            * onGotManifest
            * @description Stores received manifest data
            * @param {Object} data - Manifest data  
            * @memberOf module:common/services
            * @instance
        */
        onGotManifest: function(data){  
            this.manifest = data;
        }

	};

    /** 
    * @global 
    */
	var oServices = new Services();	
	return (oServices);      
   
});