(function ($){ function IllegalArgumentException(msg){ this.msg = msg; } function UndefinedException(msg){ this.msg = msg; } $.fn.filter = function (options){ if (typeof currentPage == 'undefined') { throw new UndefinedException("the variable 'currentPage' MUST be defined in the same scope of this plugin") } var defaults = { url: null , dataType: "json", table: null , buildEntries: null , queryParam: 'q', pageParam: "page", before: null , after: null } ; options = $.extend(defaults, options); if (_AN_Read_url("url", options) == null ) { _AN_Write_url("url", options, false , $(this).attr('data-url')); } if (options.table == null ) { options.table = $(this).attr('data-table'); } var notNull = { url: "no specified URL to filter resources from", dataType: "no sepcified dataType to parse resources to", table: "no specified element selector to populate filtered resources with", buildEntries: "no specified function to build HTML from returned data with", queryParam: "no specfied parameter name for a query string to filter resources with", pageParam: "no specified parameter name for a page number to paginate data with"} ; for (var key in notNull){ if (defaults[key] == null ) { throw new IllegalArgumentException(notNull[key]) } } $(this).keyup(function (){ currentPage = 1; var query = $(this).val(); var request = { url: _AN_Read_url("url", options), data: { } , type: "GET", dataType: options.dataType, success: function (data){ var table = $(options.table); table.html(options.buildEntries(data)); } } ; if (options.before != null ) { options.before(request); } var data = request.data; data[options.queryParam] = query; data[options.pageParam] = currentPage; $.ajax(request).always(function (){ if (options.after != null ) { options.after(); } } ); } ); } ; } (jQuery));