2012-08-30 40 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,{ });'它不起作用。 –