Skip to content

Implements two public methods: toggle allows toggling the button via js. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ErwinM
Copy link

@ErwinM ErwinM commented Jan 25, 2014

SilentToggle does the same only without triggering a change event. This solves #7.

@ErwinM
Copy link
Author

ErwinM commented Jan 26, 2014

Added a third 'redraw' method that brings the button back in sync with the checkbox.

@mikemarty
Copy link

@ErwinM, I'm a total newbie. Can you tell me how to trigger the 'redraw' method? What do I call after this?

$("#switch1 input").prop('checked', true);

@olance
Copy link
Owner

olance commented Mar 20, 2014

@ErwinM thanks for your PR, however I wonder if this is really the way to go?

Shouldn't we listen to the change event of the underlying checkbox and silently update the switch button when the checkbox state changes?
We should code something against entering an infinite loop of events though, but that doesn't seem unfeasible.

@mikemarty
Copy link

@olance, do you know how to trigger the redraw function that @ErwinM added? I'm using this for my kid's science fair project that is coming up! (smartphone-controlled home automation).

@olance
Copy link
Owner

olance commented Mar 21, 2014

@mikem1977

$("#switch1 input").switchButton("redraw");

You'll have to check out @ErwinM's code though

@mikemarty
Copy link

Yup, I've tried that:

$("#switch1 input").switchButton("redraw");

I keep getting:

Uncaught TypeError: Object [object Object] has no method 'switchButton'

The selector is definitely returning something, and I do this to turn the checkbox into the switchButton (which works fine):

            $("#switch1 input").switchButton({
                    show_labels: true,
                    width: 100,
                    height: 80,
                    button_width: 50
            });

One of these days I'll figure out this whole jQuery/Javascript world. Cool widget though.

@ErwinM
Copy link
Author

ErwinM commented Mar 23, 2014

@mikem1977 Are you sure you checked out my fork of SwitchButton from my Github repo?

@olance I agree what you propose is nicer. However, this suited my needs and I thought perhaps it would help other. There is no harm in having a redraw method? Up to you of course.

@mikemarty
Copy link

@ErwinM, I got it to work.

I had to do something like:

"""
var $myswitch;

function init_switches() {
$myswitch = $("#switch1 input").switchButton({
show_labels: true,
width: 100,
height: 80,
button_width: 50
});
}

function update_switch_state() {
$myswitch.switchButton("redraw");
// $("#switch1 input").switchButton("redraw") does NOT work
}

"""

I'm quite the JQuery/Javascript newbie, so I can't really understand/explain why using a variable works instead of the selector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants