Extending a jquery plugin or building mixins on top of a base plugin? -


i have basic plugin written according jquery authoring guide. plugin implements basic features ul of elements (activate, deactivate , search).

jquery.myplugin.core.js

(function($) {     var methods = {         init : function(opts){             var options = $.extend({}, $.fn.list.defaults, opts);             return this.each(function(i) {                 ...                 methods.search("my search query"));             });          },          deactivate : function(){ ... },          activate : function(){ ... },          search : function(query){ ... },     };     $.fn.list = function(method){         if ( methods[method] ) {             return methods[method].apply( this, array.prototype.slice.call( arguments, 1 ));         } else if ( typeof method === 'object' || ! method ) {             return methods.init.apply( this, arguments );         } else {             $.error( 'method ' +  method + ' not exist on jquery.list' );         } };     $.fn.list.defaults = {...}; })(jquery); 

i have numerous lists in app, each performing different actions - sharing features. example; 2 lists might considered deleteable such items can deleted them. there way write these feature sets mixins or extended plugins of core plugin?

so have:

  • jquery.myplugin.core.js
  • jquery.myplugin.editable.js
  • jquery.myplugin.deletable.js
  • ...

i had @ best way extend jquery plugin made me think of:

jquery.myplugin.deletable.js

(function($) {      var extensionmethods = {         delete : function(elem){ console.log("deleting "+elem); }     };  $.fn.list.deletable = function(){ return $.extend(true, $.fn.list.prototype, extensionmethods); };  })(jquery); 

which thought allow me do:

>>> $('#list').list.deletable(); >>> $('#list').list.deletable("delete", elem); >>> $('#list').list.deleteable("search", "some element"); # method core 

but nothing happens i'm sure there i'm not understanding correctly. can me out here?

you create plugin calls original plugin,

http://jsfiddle.net/jlcrm/

(function($){     var methods = {         "delete": function() {             console.log("delete");             return this;         }     };     $.fn.deleteable = function(method) {         if (methods[method]) {             return methods[method].apply(this, array.prototype.slice.call(arguments, 1));         } else if (typeof method === 'object' || !method) {             return $.fn.list.apply(this,arguments);         } else {             try {                 return $.fn.list.apply(this,arguments);             } catch (e) {                 $.error('method ' + method + ' not exist on jquery.delete');             }         }     }; })(jquery);  $(document).list().list("search") $(document).deleteable().deleteable("delete"); 

there typos in code, fixed them in fiddle.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -