Last active
August 31, 2018 19:59
-
-
Save dtomasi/4392569f17481e282376 to your computer and use it in GitHub Desktop.
ContentLoader loads HTML from given UrlObject via Ajax.
It returns all data via complete-Function if all ist recieved. onlyContainer-Option allows to fetch a specified Element from Url eg. returns only specified element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JQuery Plugin ContentLoader | |
* @copyright tomasiMEDIA 2013 | |
* @author Dominik Tomasi | |
* @date 09.10.13 | |
*/ | |
(function($,window){ | |
$.ContentLoader.defaultOptions = { | |
urlObject: {}, | |
onlyContainer: null, | |
complete: function(){} | |
}; | |
$.ContentLoader = function(el, options){ | |
var base = this; | |
base.$el = $(el); | |
base.el = el; | |
base.$el.data("ContentLoader", base); | |
//Constructor | |
base.init = function(){ | |
base.options = $.extend({},$.ContentLoader.defaultOptions, options); | |
base.content = {}; | |
$.each( base.options.urlObject ,function(index,url){ | |
$.get( url, function( data ) { | |
var saveData; | |
if( base.options.onlyContainer !== null ){ | |
saveData = $(data).find(base.options.onlyContainer); | |
} else { | |
saveData = data; | |
} | |
base.content[index] = saveData; | |
if( objectLength(base.content) == objectLength(base.options.urlObject)){ | |
console.log( "complete" ); | |
base.options.complete(base.content); | |
} | |
}); | |
}); | |
}; | |
// Run Constructor | |
base.init(); | |
}; | |
$.fn.ContentLoader = function(options){ | |
return this.each(function(){ | |
(new $.ContentLoader(this, options)); | |
}); | |
}; | |
window.ContentLoader = function(options){ | |
(new $.ContentLoader(this, options)); | |
}; | |
})(jQuery,window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment