Skip to main content
deleted 170 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

What about accessing jQuery Color Picker Sliders plugin methods with triggers?

There is my jQuery Color Picker Sliders plugin you can access on jsFiddle:

http://jsfiddle.net/styu/5H8cA/

I want to access some of the methods from outside of the plugins scope, namely showPopup()showPopup(), hidePopup()hidePopup() and updateColor(newcolor)updateColor(newcolor).

Initialization

<span class="cp"></span>

<script>
    var cp = $(".cp").ColorPickerSliders({
        flat: true,
        order: {
            hsl: 1,
            preview: 2
        }
    });
</script>

What I want to do

Something like

cp.updateColor('red');

or

$(".cp").ColorPickerSliders('updateColor', 'red');

Which one is the preferred way, and how to refactor the code to accompishaccomplish it?

Any other suggestions are welcome!

Update

I achieved it using triggers, but don't know if it has any downsides or is a good way to do these kind of interaction. So I registered a custom event handler inside the plugin's code (Line 320, triggerelement is the original element the plugin is called on):

triggerelement.on('jquerycolorpickersliders.updateColor', function(e, newcolor) {
    updateColor(newcolor);
});

And call it using this code (where cp is the element the plugin is called on):

cp.trigger('jquerycolorpickersliders.updateColor', 'red');

Updated jsFiddle: http://jsfiddle.net/styu/5H8cA/3/jsFiddle.

What do you think of it? Is it an acceptable solution for the problem? Or thereIs it an acceptable solution for the problem, or is there a better one?

What about accessing jQuery plugin methods with triggers?

There is my jQuery Color Picker Sliders plugin you can access on jsFiddle:

http://jsfiddle.net/styu/5H8cA/

I want to access some of the methods from outside of the plugins scope, namely showPopup(), hidePopup() and updateColor(newcolor).

Initialization

<span class="cp"></span>

<script>
    var cp = $(".cp").ColorPickerSliders({
        flat: true,
        order: {
            hsl: 1,
            preview: 2
        }
    });
</script>

What I want to do

Something like

cp.updateColor('red');

or

$(".cp").ColorPickerSliders('updateColor', 'red');

Which one is the preferred way, and how to refactor the code to accompish it?

Any other suggestions are welcome!

Update

I achieved it using triggers, but don't know if it has any downsides or is a good way to do these kind of interaction. So I registered a custom event handler inside the plugin's code (Line 320, triggerelement is the original element the plugin is called on):

triggerelement.on('jquerycolorpickersliders.updateColor', function(e, newcolor) {
    updateColor(newcolor);
});

And call it using this code (where cp is the element the plugin is called on):

cp.trigger('jquerycolorpickersliders.updateColor', 'red');

Updated jsFiddle: http://jsfiddle.net/styu/5H8cA/3/

What do you think of it? Is it an acceptable solution for the problem? Or there is a better one?

jQuery Color Picker Sliders plugin

I want to access some of the methods from outside of the plugins scope, namely showPopup(), hidePopup() and updateColor(newcolor).

Initialization

<span class="cp"></span>

<script>
    var cp = $(".cp").ColorPickerSliders({
        flat: true,
        order: {
            hsl: 1,
            preview: 2
        }
    });
</script>

What I want to do

Something like

cp.updateColor('red');

or

$(".cp").ColorPickerSliders('updateColor', 'red');

Which one is the preferred way, and how to refactor the code to accomplish it?

I achieved it using triggers, but don't know if it has any downsides or is a good way to do these kind of interaction. So I registered a custom event handler inside the plugin's code (Line 320, triggerelement is the original element the plugin is called on):

triggerelement.on('jquerycolorpickersliders.updateColor', function(e, newcolor) {
    updateColor(newcolor);
});

And call it using this code (where cp is the element the plugin is called on):

cp.trigger('jquerycolorpickersliders.updateColor', 'red');

jsFiddle.

What do you think of it? Is it an acceptable solution for the problem, or is there a better one?

Notice removed Authoritative reference needed by styu
Bounty Ended with JohnMark13's answer chosen by styu
Notice added Authoritative reference needed by styu
Bounty Started worth 50 reputation by styu
edited title
Link
styu
  • 63
  • 10

Access internal methods from outside of What about accessing jQuery plugins scopeplugin methods with triggers?

Proposed solution for review
Source Link
styu
  • 63
  • 10

How to refactor jQuery plugin to access Access internal methods from outside of jQuery plugins scope

There is my jQuery Color Picker Sliders plugin you can access on jsFiddle:

http://jsfiddle.net/styu/5H8cA/

I want to access some of the methods from outside of the plugins scope, namely showPopup(), hidePopup() and updateColor(newcolor).

Initialization

<span class="cp"></span>

<script>
    var cp = $(".cp").ColorPickerSliders({
        flat: true,
        order: {
            hsl: 1,
            preview: 2
        }
    });
</script>

What I want to do

Something like

cp.updateColor('red');

or

$(".cp").ColorPickerSliders('updateColor', 'red');

Which one is the preferred way, and how to refactor the code to accompish it?

Any other suggestions are welcome!

Update

I achieved it using triggers, but don't know if it has any downsides or is a good way to do these kind of interaction. So I registered a custom event handler inside the plugin's code (Line 320, triggerelement is the original element the plugin is called on):

triggerelement.on('jquerycolorpickersliders.updateColor', function(e, newcolor) {
    updateColor(newcolor);
});

And call it using this code (where cp is the element the plugin is called on):

cp.trigger('jquerycolorpickersliders.updateColor', 'red');

Updated jsFiddle: http://jsfiddle.net/styu/5H8cA/3/

What do you think of it? Is it an acceptable solution for the problem? Or there is a better one?

How to refactor jQuery plugin to access methods from outside of plugins scope

There is my jQuery Color Picker Sliders plugin you can access on jsFiddle:

http://jsfiddle.net/styu/5H8cA/

I want to access some of the methods from outside of the plugins scope, namely showPopup(), hidePopup() and updateColor(newcolor).

Initialization

<span class="cp"></span>

<script>
    var cp = $(".cp").ColorPickerSliders({
        flat: true,
        order: {
            hsl: 1,
            preview: 2
        }
    });
</script>

What I want to do

Something like

cp.updateColor('red');

or

$(".cp").ColorPickerSliders('updateColor', 'red');

Which one is the preferred way, and how to refactor the code to accompish it?

Any other suggestions are welcome!

Access internal methods from outside of jQuery plugins scope

There is my jQuery Color Picker Sliders plugin you can access on jsFiddle:

http://jsfiddle.net/styu/5H8cA/

I want to access some of the methods from outside of the plugins scope, namely showPopup(), hidePopup() and updateColor(newcolor).

Initialization

<span class="cp"></span>

<script>
    var cp = $(".cp").ColorPickerSliders({
        flat: true,
        order: {
            hsl: 1,
            preview: 2
        }
    });
</script>

What I want to do

Something like

cp.updateColor('red');

or

$(".cp").ColorPickerSliders('updateColor', 'red');

Which one is the preferred way, and how to refactor the code to accompish it?

Any other suggestions are welcome!

Update

I achieved it using triggers, but don't know if it has any downsides or is a good way to do these kind of interaction. So I registered a custom event handler inside the plugin's code (Line 320, triggerelement is the original element the plugin is called on):

triggerelement.on('jquerycolorpickersliders.updateColor', function(e, newcolor) {
    updateColor(newcolor);
});

And call it using this code (where cp is the element the plugin is called on):

cp.trigger('jquerycolorpickersliders.updateColor', 'red');

Updated jsFiddle: http://jsfiddle.net/styu/5H8cA/3/

What do you think of it? Is it an acceptable solution for the problem? Or there is a better one?

Source Link
styu
  • 63
  • 10
Loading