Skip to content

Instantly share code, notes, and snippets.

@cmarkle27
Created August 4, 2012 18:48
Show Gist options
  • Save cmarkle27/3259276 to your computer and use it in GitHub Desktop.
Save cmarkle27/3259276 to your computer and use it in GitHub Desktop.
Advanced jQuery Plugin
// plug it in, plug it in
(function($) {
var paraCount = function() {
return {
options: {
itemClass: 'awesome',
baseUrl: "/",
navPath: "",
callMe: function() {
console.log("ring, ring...");
}
},
// --------------------------------------------------------------------
init: function(options, element) {
this.options = $.extend(this.options, options);
this.elem = element;
this.attachEvents();
},
// --------------------------------------------------------------------
someMethod: function() {
alert('someMethod called');
return $(this.elem); // for chaining
},
// --------------------------------------------------------------------
attachEvents: function() {
var self = this;
/////////////////////////
$(self.elem).on("hover", function(e) {
var $currentElement = $(this),
$paragraphs;
$paragraphs = $(self.elem).find("p");
$paragraphs.addClass(self.options.itemClass).css({ "color" : "red" });
self.paragraphCount = $paragraphs.length;
});
/////////////////////////
$(self.elem).on("click", function(e) {
var request = $.ajax({
url: self.options.baseUrl,
type: "POST",
data: { "id" : 7 },
dataType: "html"
});
e.preventDefault();
request.success(function(msg) {
console.log(msg);
});
request.error(function(jqXHR, textStatus) {
console.warn("Request failed.");
});
request.complete(function(jqXHR, textStatus) {
console.log("done!");
self.options.callMe();
});
});
}
};
};
// --------------------------------------------------------------------
$.fn.paraCount = function(options) {
// create an instance for each element in the set
return this.each(function() {
var myParaCount = new paraCount();
myParaCount.init(options, this);
// this lets us call methods on the selector
$(this).data("paraCount", myParaCount);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment