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');
What do you think of it? Is it an acceptable solution for the problem, or is there a better one?