2014-12-06 16 views
6

Önceki soruda, yok etme yönteminin istemediğim bir şeyi yaptığı bootstrap select eklentisi hakkında konuşuyordum. Eklentiyi manuel olarak düzenliyorum ama bu iyi bir uygulama değil.Bootstrap select eklentisini genişlet

Bootstrap select destroy removes the original select from DOM

Ben tam olarak wat istediğim yapmak, böylece özel bir yöntemle eklenti uzatmak istiyoruz.

Ben folloing yöntemiyle eklentisi uzatmak:

$.extend(true, $.fn.selectpicker.prototype, { 
    customRemove: function() { 
     this.$newElement.remove(); 
     this.$element.show(); 
    } 
}); 

Bu önyükleme komut seç dosyası altında başka bir js dosyasında yer almaktadır.

Bu yeni yöntemi nasıl ararım?

$('select#begin' + _week + _day).selectpicker("customRemove"); 

veya

$('select#begin' + _week + _day).selectpicker().customRemove(); 

bir şey eksik: Ben başarılı olamadı aşağıdaki çalıştı?

önyükleme seçeneğini eklentisinde yok fonksiyonunun orijinal yöntemi:

remove: function() { 
    this.$newElement.remove(); 
    this.$element.show(); 
} 
+0

'selectpicker()' dönüş 'this' mu? Eğer değilse, yöntemi zincirleyemezsiniz. –

+0

Eklentide şunu görüyorum '' geri dön ''evet – Mivaweb

+0

selectpicker.prototype.customRemove(); – Dave

cevap

1

Yapabilirsin vekil gibi bir mixin kapatma kullanarak kendi fonksiyonu ile fonksiyonu:

(function() 
{ 
    // grab a reference to the existing remove function 
    var _remove = $.fn.selectpicker.prototype.remove; 

    // anything declared in here is private to this closure 
    // so you could declare helper functions or variables 
    // ... 

    // extend the prototype with your mixin 
    $.extend(true, $.fn.selectpicker.prototype, 
    { 
    // this will replace the original $.fn.selectpicker.prototype.remove function 
    remove: function() 
    { 
     // call whatever code you want here 
     // ... 
     // ... like grabbing the DOM element 
     // ... 
     // then call the original remove function 
     _remove.apply(this, arguments); 

     // then call whatever you want here, like re-adding the DOM element 
    } 
    }); 
}()); 

Not: Bu Tüm Bootstrap'ın seçtiği ve davranışını değiştirdiği prototipi aşacak. Bence

1

, diğer sorunuza doğru cevabı var:
https://stackoverflow.com/a/31852632/4928642

jQuery.fn.selectpicker.Constructor.prototype.customRemove = function() { 
    this.$newElement.remove(); 
    this.$element.show(); 
}; 

sonra

$smth.selectpicker("customRemove");