Skip to content
Browse files

Allow setting default active toggle + added support for optional befo…

…reToggle function
  • Loading branch information...
1 parent dd1fc43 commit bafc03740d977425c22c41012fe4c50cf0614cbb @KyleAMathews committed Jun 1, 2012
Showing with 21 additions and 6 deletions.
  1. +21 −6 toggle/toggle_view.coffee
View
27 toggle/toggle_view.coffee
@@ -1,23 +1,38 @@
ToggleTemplate = require 'widgets/toggle/toggle'
+
module.exports = class ToggleView extends Backbone.View
events:
'mousedown button': 'toggleActive'
render: ->
@$el.html ToggleTemplate( buttons: @options.buttons )
- @$('button').eq(0).addClass 'active'
+ @$("button[data-machinename='#{ @options.active }']").addClass('active')
# Add fastClick support.
if $.fn.fastClick?
@$('button').fastClick (e) =>
@toggleActive(e)
@
+ setActive: (machinename) ->
+ @$('button').removeClass 'active'
+ @$("button[data-machinename='#{ machinename }']").addClass('active')
+
toggleActive: (e) ->
- unless $(e.target).hasClass 'active'
- @$('button').removeClass 'active'
- $(e.target).addClass 'active'
- @trigger 'toggle', $(e.target).data('machinename')
+ # Parent views using this widget can pass in a beforeToggle function
+ # to validate or do other checks before allowing the toggle to
+ # continue.
+ if @options.beforeToggle
+ @options.beforeToggle (e), @_toggle
+ else
+ @_toggle(e)
+
+ _toggle: (error, e) =>
+ unless error
+ unless $(e.target).hasClass 'active'
+ @$('button').removeClass 'active'
+ $(e.target).addClass 'active'
+ @trigger 'toggle', $(e.target).data('machinename')
-exports.ToggleView.prototype = _.extend exports.ToggleView.prototype, Backbone.Events
+module.exports.prototype = _.extend module.exports.prototype, Backbone.Events

0 comments on commit bafc037

Please sign in to comment.
Something went wrong with that request. Please try again.