Ok so first off here is my very basic jQuery plugin
(function ($){
$.fn.greenify = function (options) {
var settings = $.extend({
// Thes
Creating jQuery plugins along with TypeScript can get a bit messy. I personally prefer keeping the jQuery plugin chaining syntax, mostly for consistency and maintainability .
So, after the module declaration you can basically wrap around your implementation extending the jQuery prototype as:
module Coloring {
interface IGreenifyOptions {
color: string;
backgroundColor: string;
}
export class GreenifyOptions implements IGreenifyOptions {
// Fields
color: string;
backgroundColor: string;
constructor(color: string, backgroundColor: string) {
this.color = color;
this.backgroundColor = backgroundColor;
}
}
export class Greenify {
// Fields
element: JQuery;
options: GreenifyOptions;
constructor(element: JQuery, options: GreenifyOptions) {
this.element = element;
this.options = options;
this.OnCreate();
}
OnCreate() {
this.element.css('color', this.options.color).css('background-color', this.options.backgroundColor);
}
}
}
//jquery plugin wrapper.
;
(function(w, $) {
//no jQuery around
if (!$) return false;
$.fn.extend({
Coloring: function(opts) {
//defaults
var defaults: Coloring.GreenifyOptions = new Coloring.GreenifyOptions('#0F0', '#000');
//extend the defaults!
var opts = $.extend({}, defaults, opts)
return this.each(function() {
var o = opts;
var obj = $(this);
new Coloring.Greenify(obj, o);
});
}
});
})(window, jQuery);
Fetching the plugin as:
$(function() {
var $a = $('a').Coloring();
var $div = $('div').Coloring({
color: '#F0F',
backgroundColor: '#FFF'
});
var $div = $('strong').Coloring({
color: '#gold',
backgroundColor: 'pink'
});
});
Demo