simontabor / jquery-toggles

jQuery plugin to make easy toggle buttons
http://simontabor.com/labs/toggles
MIT License
362 stars 67 forks source link

Feature request: Set state without triggering on toggle event #47

Closed ocgltd closed 9 years ago

ocgltd commented 9 years ago

There is a common use case where the toggle button displays the state of an underlying object. Sometimes the state of that object changes and the toggle should display the new state.

At the moment the only way to do this is called toggle(newstate) however this also causes the on toggle event to fire which can create a coding headache to seperate user generated triggers from back-end state changes.

I would like to request a way to change the state WITHOUT causing the on toggle event to fire

ocgltd commented 9 years ago

Not a GIT guru...but if someone wants to add this code it will add a suitable display function:

// Set to desire state WITHOUT triggering event, and optional animation Toggles.prototype.display = function(state, doAnimate) { var self = this; //console.log("in plugin display to: "+state); // check we arent already in the desired state if (self['active'] === state) return;

var active = self['active'] = !self['active'];

self.el.data('toggle-active', active);

self.els.off.toggleClass('active', !active); self.els.on.toggleClass('active', active); self.checkbox.prop('checked', active);

if (self.selectType) return;

var margin = active ? 0 : -self.w + self.h;

// move the toggle! self.els.inner.stop().animate({ 'marginLeft': margin }, (doAnimate?self.opts['animate']:0) ); };