0

I am quite new to jQuery plugins and I don't understand the architecture of the bootstrap-slider plugin.

What is actually returned when you create the slider?

var mySlider = $('.slider').slider({...});

I would expect the Slider class (object) but I can not access it's methods directly, instead I have to do this:

mySlider.slider('setValue',4);

Why should I call Slider's methods as string and not directly? What I would expect is:

var mySlider = $('.slider').slider({...});?
mySlider.setValue(4);

Again I am new to this, is it some kind of ugly pattern or did I understand it wrongly? Can someone please explain it please?

Thanks

Talha Masood
  • 993
  • 1
  • 7
  • 22
daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77

2 Answers2

1

Basically you can calling an anonymous jQuery function on a selector. When you call $('.slider') you are creating a jQuery object where the methods and parameters are stored.

JQuery plugin patterns is a great place to start. I would checkout this article by @addyosmani http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/

TorontoTim
  • 11
  • 1
0

Apparently the architecture was inspired by How to create a jQuery plugin with methods?.

IMHO this is very bad architecture. It is error prone, can not be strong typed (with TypeScript) or checked for integrity at compile time. There is a reason we don't write code in strings.

Community
  • 1
  • 1
daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77