{"version":3,"sources":["jquery.data-remote.js"],"names":["$","window","undefined","fn","dataRemote","successCallback","$target","options","response","source","template","html","$template","find","hide","debug","handlebars","length","Handlebars","compile","validPlacements","placementIsValid","indexOf","placement","errorCallback","error","fetch","url","settings","$element","element","target","loaderImg","prepend","String","ajax","data","type","dataType","cache","success","call","complete","status","_error","opts","arguments","defaults","eventType","oneAndDone","before","extend","this","each","idx","_options","hasAttribute","method","evt","preventDefault","name","value","output","console","log","jQuery"],"mappings":"AAUA,cAEC,SAAWA,EAAGC,EAAQC,GAGrBF,EAAEG,GAAGC,WAAa,WAqChB,QAASC,GAAgBC,EAASC,EAASC,GACzC,GAAIC,GACAC,EACAC,EACAC,CAEJN,GAAQO,KAAK,iBAAiBC,OAE1BP,EAAQQ,OACVf,EAAEG,GAAGC,WAAWW,MAAMT,EAASE,GAI7BD,EAAQS,cAAe,GAASV,EAAQO,KAAK,uCAAuCI,QACtFL,EAAYL,EAAQG,SAAWV,EAAEO,EAAQG,UAAYJ,EAAQO,KAAK,uCAClEJ,EAASG,EAAUD,OACnBD,EAAWQ,WAAWC,QAAQV,GAC9BE,EAAOD,EAASF,IAEhBG,EAAOH,CAMT,IAAIY,IAAmB,OAAQ,SAAU,UAAW,QAAS,UACzDC,EAAkE,KAA/CD,EAAgBE,QAAQf,EAAQgB,UAClDF,IAGLf,EAAQC,EAAQgB,WAAWZ,GAa7B,QAASa,GAAclB,EAASC,EAASC,EAAUiB,GACjDnB,EAAQO,KAAK,iBAAiBC,OAE1BP,EAAQQ,OACVf,EAAEG,GAAGC,WAAWW,MAAMT,EAASE,EAAUiB,GAgB7C,QAASC,GAAMC,EAAKC,GAClB,GAAIC,GAAWD,EAASE,QACpBxB,EAAUsB,EAASG,OACnBxB,EAAUqB,EAASrB,OAGnBA,GAAQyB,WACV1B,EAAQ2B,QAAQC,SAAW,4DAAmE3B,EAAQyB,UAAY,6BAKpHhC,EAAEmC,MACAR,IAAKA,EACLS,KAAM7B,EAAQ6B,KACdC,KAA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KAAK,SAAW7B,EAAQQ,MACvFwC,EAASN,WAAanB,EAAQ0B,aAAa,qBAAuB3B,EAASO,KAAK,gBAAkB7B,EAAQ0C,WAC1GM,EAASvC,WAAac,EAAQ0B,aAAa,mBAAqB3B,EAASO,KAAK,cAAgB7B,EAAQS,UAGtG,IAAIV,GAAUiD,EAASxB,OAAS/B,EAAEuD,EAASxB,QAAUF,CAIrD,IAA2B,SAAvB0B,EAASP,UACXO,EAASL,OAAOT,KAAKZ,EAAUvB,GAG/BoB,EAAM6B,EAAS5B,KACbG,QAASD,EACTE,OAAQzB,EACRC,QAASgD,QAEN,CAEL,GAAIE,GAASF,EAASN,WAAa,MAAQ,IAG3CpB,GAAS4B,GAAQF,EAASP,UAAW,SAAUU,GAC7CA,EAAIC,iBAM0C,MAAzC,QAAS,UAAUrC,QAAQoC,EAAIrB,QAClCkB,EAASnB,KAAKgB,KAAKQ,MAAQR,KAAKS,OAIlCN,EAASL,OAAOT,KAAKZ,EAAUvB,GAE/BoB,EAAM6B,EAAS5B,KACbG,QAASD,EACTE,OAAQzB,EACRC,QAASgD,UAUnBvD,EAAEG,GAAGC,WAAWW,MAAQ,SAAUe,EAASgC,EAAQrC,GAC7CxB,EAAO8D,SAAW9D,EAAO8D,QAAQC,MACnC/D,EAAO8D,QAAQC,IAAI,aACnB/D,EAAO8D,QAAQC,IAAIlC,GACnB7B,EAAO8D,QAAQC,IAAI,cACnB/D,EAAO8D,QAAQC,IAAIF,GACfrC,IACFxB,EAAO8D,QAAQC,IAAI,eACnB/D,EAAO8D,QAAQC,IAAIvC,IAErBxB,EAAO8D,QAAQC,IAAI,mBAGtBC,OAAQhE","file":"jquery.data-remote.min.js","sourcesContent":["/**\n * jQuery Data Remote Plugin\n * http://github.com/codonnell822/jquery-dataremote\n * Copyright 2014-2015, Chris O'Donnell\n *\n * jQuery plugin that simplifies the common task of making api/remote requests\n * and injecting the response to the page. Optionally leverages templating\n * engines (handlebars). Inpired by ruby on rails' unobtrusive scripting adapter\n * for jQuery (https://github.com/rails/jquery-ujs).\n */\n'use strict';\n\n;(function ($, window, undefined) {\n 'use strict';\n\n $.fn.dataRemote = function () {\n var opts = arguments[0] === undefined ? {} : arguments[0];\n\n // Default Settings\n var defaults = {\n url: null, // request url\n data: {}, // request data\n debug: false,\n eventType: 'load', // js event type to fire data request on (load, click, submit, mouseover, custom, etc.)\n dataType: 'json', // data type that's expected in response from your request\n type: 'GET', // type of request (currently only supports GET)\n target: '', // selector of the element where you want your response injected\n handlebars: false, // use handlebars to render the response?\n template: '', // handlebar template selector. by default it will look inside target\n placement: 'html', // where to inject response relative to target (jquery DOM insertion methods )\n loaderImg: null, // target selector for data response\n oneAndDone: true, // whether to remove the event binding after the first time running\n success: successCallback, // gets passed 3 parameters ($target, options, response)\n error: errorCallback, // gets passed 4 parameters ($target, options, response, error)\n complete: function complete($target) {}, // callback fires after the request is made (on success OR error)\n before: function before($target) {} // callback fires directly before the request is made\n };\n\n // Extend our default options with those provided when instantiating\n var options = $.extend({}, defaults, opts);\n\n /**\n * Default success callback for ajax requests.\n *\n * Handles hiding of loader image, debugging (if option is set),\n * and handling the ajax requests' response (levering handlebars if option\n * is set or handlebars template is found inside the target element).\n *\n * @param {jQuery} $target jquery object containing the target element for the ajax response\n * @param {object} options plugin options set during instantiation\n * @param {object} response data response from the ajax request\n */\n function successCallback($target, options, response) {\n var source;\n var template;\n var html;\n var $template;\n\n $target.find('.loader-image').hide();\n\n if (options.debug) {\n $.fn.dataRemote.debug($target, response);\n }\n\n // Use handlebars if the option is set to true *or* if a handlebars template is found\n if (options.handlebars !== false || $target.find('[type=\"text/x-handlebars-template\"]').length) {\n $template = options.template ? $(options.template) : $target.find('[type=\"text/x-handlebars-template\"]');\n source = $template.html();\n template = Handlebars.compile(source);\n html = template(response);\n } else {\n html = response;\n }\n\n // use proper jQuery method based on append option\n // if append == true, append results to target element,\n // or else replace inner html with results\n var validPlacements = ['html', 'append', 'prepend', 'after', 'before'];\n var placementIsValid = validPlacements.indexOf(options.placement) !== -1;\n if (!placementIsValid) {\n return;\n }\n $target[options.placement](html);\n }\n\n /**\n * Default error callback for ajax requests\n *\n * Hides the loader image, triggers debugging if it's turned on.\n *\n * @param {jQuery} $target jquery object containing the target element for the ajax response\n * @param {object} options plugin options set during instantiation\n * @param {object} response data response from the ajax request\n * @param {string} error textual portion of the HTTP status, i.e. \"Not Found\" or \"Internal Server Error.\"\n */\n function errorCallback($target, options, response, error) {\n $target.find('.loader-image').hide();\n\n if (options.debug) {\n $.fn.dataRemote.debug($target, response, error);\n }\n }\n\n /**\n * Private helper method for making the ajax request\n *\n * @param {object} url request url\n * @param {object} settings additional settings for the request\n * {\n * @param {jQuery} element jQuery object containing the current data remote element\n * @param {object} options data remote plugin options for current data remote element\n * @param {jQuery} $target jQuery object containing the target element to inject response\n * }\n * @return {void}\n */\n function fetch(url, settings) {\n var $element = settings.element;\n var $target = settings.target;\n var options = settings.options;\n\n // display loader image while retrieving content\n if (options.loaderImg) {\n $target.prepend(String() + '
' + '\"Loading...\"' + '
');\n }\n\n // make the ajax request. Trigger the callbacks using $element as the\n // context, making the value of `this` for the callbacks the $element\n $.ajax({\n url: url,\n data: options.data,\n type: options.type,\n dataType: options.dataType,\n cache: true,\n success: function success(response) {\n options.success.call($element, $target, options, response);\n options.complete.call($element, $target);\n },\n error: function error(response, status, _error) {\n options.error.call($element, $target, options, response, _error);\n options.complete.call($element, $target);\n }\n });\n }\n\n return this.each(function (idx, element) {\n var $element = $(element);\n\n // Create a local copy of the options for each element. This will allow each\n // individual element to override options from its' data attributes.\n var _options = $.extend({}, options);\n _options.dataType = $element.data('response-type') || options.dataType;\n _options.eventType = $element.data('event-type') || options.eventType;\n _options.type = $element.data('type') || options.type;\n _options.url = $element.data('url') || options.url;\n _options.data = $element.data('data') ? $element.data('data') : options.data;\n _options.template = $element.data('template') || options.template;\n _options.placement = $element.data('response-placement') || options.placement;\n _options.target = $element.data('target') || options.target;\n _options.debug = element.hasAttribute('data-debug') ? $element.data('debug') : options.debug;\n _options.oneAndDone = element.hasAttribute('data-one-and-done') ? $element.data('one-and-done') : options.oneAndDone;\n _options.handlebars = element.hasAttribute('data-handlebars') ? $element.data('handlebars') : options.handlebars;\n\n // if no target selector is given, default to actual element\n var $target = _options.target ? $(_options.target) : $element;\n\n // if event type is 'load', execute the request immediately otherwise,\n // execute ajax request on specified type (click, submit, mouseover, etc.)\n if (_options.eventType === 'load') {\n _options.before.call($element, $target);\n\n // execute ajax request immediately\n fetch(_options.url, {\n element: $element,\n target: $target,\n options: _options\n });\n } else {\n // use proper jQuery method based on oneAndDone option\n var method = _options.oneAndDone ? 'one' : 'on';\n\n // bind to specific event type\n $element[method](_options.eventType, function (evt) {\n evt.preventDefault();\n\n // if you're watching on keyup or change events, let's assume you want to\n // send the value of the element as a query parameter.\n // Think autosuggest search boxes.\n // \n if (['keyup', 'change'].indexOf(evt.type) !== -1) {\n _options.data[this.name] = this.value;\n }\n\n // execute before request callback\n _options.before.call($element, $target);\n\n fetch(_options.url, {\n element: $element,\n target: $target,\n options: _options\n });\n });\n } // end if else (_options.eventType)\n }); // end this.each\n }; // end $.fn.dataRemote\n\n /**\n * Helper function for debugging\n */\n $.fn.dataRemote.debug = function (element, output, error) {\n if (window.console && window.console.log) {\n window.console.log('Element: ');\n window.console.log(element);\n window.console.log('Response: ');\n window.console.log(output);\n if (error) {\n window.console.log(' -> Error: ');\n window.console.log(error);\n }\n window.console.log('------------');\n }\n };\n})(jQuery, window);\n//# sourceMappingURL=jquery.data-remote.js.map"],"sourceRoot":"/source/"}