UNPKG

intelia-jquery-plugin

Version:

Intelia client for product recommendation and loyalty programms

113 lines (98 loc) 3.47 kB
/*! * intelia-jquery-plugin 1.0.0 * Intelia client for product recommendation and loyalty programms * * Created by Intelia * * @license MIT */ (function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else if (typeof module === 'object' && module.exports) { // Node/CommonJS module.exports = function (root, jQuery) { if (jQuery === undefined) { // require('jQuery') returns a factory that requires window to // build a jQuery instance, we normalize how we use modules // that require this pattern but the window provided is a noop // if it's defined (how jquery works) if (typeof window !== 'undefined') { jQuery = require('jquery'); } else { jQuery = require('jquery')(root); } } factory(jQuery); return jQuery; }; } else { // Browser globals factory(jQuery); } }(function ($) { "use strict"; // Default Options var defaults = { // TODO: ADD YOUR DEFAULT OPTIONS HERE block: 0, host: 'https://cdp.plataformaintelia.com', successCallback: null, current_product: null, customer: null, }; class InteliaPluginconstructor(element, options) { // Merge user settings with default this.options = $.extend(true, {}, defaults, options); // Main container element this.main = $(element); // Initial load this._init(); } // Initial Method _init() { // Plugin init and logic console.log(this.options); this.getRecommendations(); } getRecommendations() { var settings = { "url": this.options.host+"/api/v2/related_blocks/"+this.options.block+"/results", "method": "GET", "timeout": 0, "headers": { "Authorization": "Bearer "+this.options.apikey, }, }; $.ajax(settings).done(function (response) { //console.log(response); // aquí debería de ejecutarse un callback this.options.successCallback(response); }); } } // Wrapper for the plugin $.fn.inteliaPlugin = function (options) { var pluginName = "inteliaPlugin"; if (options === undefined || typeof options === 'object') { return this.each(function () { if (!$.data(this, pluginName)) { $.data(this, pluginName, new InteliaPlugin(this, options)); } }); } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { let instance = $.data(this[0], pluginName); if (options === 'destroy') { $.data(this, pluginName, null); } if (instance instanceof InteliaPlugin && typeof instance[options] === 'function') { return instance[options].apply(instance, Array.prototype.slice.call(arguments, 1)); } else { return this; } } }; }));