2011-08-09 25 views
6

Tôi đang cố tạo một tiện ích jQuery mở rộng từ ui.slider. Tôi muốn có một phương pháp tùy chỉnh thực hiện trên sự kiện 'slide'. Tôi cố gắng trọng lựa chọn của cha mẹ giống như khi sử dụng các widget trượt bình thường, nhưng tôi chạy vào vấn đề này với phạm vi biến:Mở rộng tiện ích jquery ui - cách truy cập sự kiện phụ huynh

$.widget("ui.myslider", $.ui.slider, { 
    _create: function() { 
     this.foo = "bar"; 

     // Outputs "bar" 
     this._mySlide(); 

     // Outputs "undefined" when triggered 
     this.options.slide = this._mySlide; 

     $.ui.slider.prototype._create.apply(this, arguments); 
    }, 
    _mySlide: function() { 
     alert(this.foo); 
    } 
} 

Làm thế nào tôi có thể kích hoạt chức năng của tôi trên các sự kiện trượt VÀ có quyền truy cập vào biến của tôi?

Edit: Liên kết đến ui.slider nguồn: https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.slider.js

Edit: Giải pháp

$.widget("ui.myslider", $.ui.slider, { 
    _create: function() { 
     $.ui.slider.prototype._create.apply(this, arguments); 
     this.foo = "bar"; 
    }, 
    _slide: function() { 
     $.ui.slider.prototype._slide.apply(this, arguments); 
     alert(this.foo); 
    } 
} 

Trả lời

9

Trong jQuery UI> = 1.9, bạn có thể sử dụng phương pháp _super thay vì:

_slide: function() { 
    this._super(); 
    // equivalent to $.Widget.prototype._slide.call(this); 
} 

hoặc superApply:

_slide: function() { 
    this._superApply(arguments); 
    // equivalent to $.Widget.prototype._slide.apply(this, arguments); 
} 
Các vấn đề liên quan