Best way to override method in jquery library

Suppose I want to change the rendering function in the bootstrap-typeahead library. http://twitter.github.com/bootstrap/assets/js/bootstrap-typeahead.js

One way to activate this goal is to do:

_.extend($.fn.typeahead.Constructor.prototype, { render: function (items) { // some code } }); 

Suppose I want to override the render function only for a specific element, such as ...

 element.typeahead({ minLength: 3, source: getSource }); 

How to override the rendering function to be valid only for this element?

PS:
I use jquery and underline

+4
source share
1 answer

Perhaps you can clone it first?

 $.fn.typeaheadCustom = $.fn.typeahead; _.extend($.fn.typeaheadCustom.Constructor.prototype, { render: function (items) { // some code } }); element.typeaheadCustom({ minLength: 3, source: getSource }); 

UPDATE

You may need deep cloning:

 $.fn.typeaheadCustom = $.extend(true, $.fn.typeahead, {}); 
+1
source

All Articles