2012-08-30 92 views
4

假設我想要更改bootstrap-typeahead庫中的渲染函數。活躍這一目標 http://twitter.github.com/bootstrap/assets/js/bootstrap-typeahead.js在jQuery庫中重新定義方法的最佳方式

一種方法是使:

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

讓我們假設我要重新定義渲染功能只爲特定元素像...

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

我應該如何重新定義渲染函數爲了只對這個元素有效?

P.S:
我使用jQuery和強調

回答

1

也許你可以先克隆嗎?

$.fn.typeaheadCustom = $.fn.typeahead; 

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

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

UPDATE

您可能需要深克隆它:

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

我試過,但它不工作,因爲'$ .fn.typeahead.Constructor.prototype.render'是更改 –

+0

也許你需要深入克隆'$ .fn.typeaheadCustom = $ .extend(true,$ .fn.typeahead,{});' – David

+0

也可以使'$ .extend(true,$ .fn.typeahead,{ });'它不起作用。 –