Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…reToggle function
  • Loading branch information...
commit bafc03740d977425c22c41012fe4c50cf0614cbb 1 parent dd1fc43
@KyleAMathews authored
Showing with 21 additions and 6 deletions.
  1. +21 −6 toggle/toggle_view.coffee
View
27 toggle/toggle_view.coffee
@@ -1,4 +1,5 @@
ToggleTemplate = require 'widgets/toggle/toggle'
+
module.exports = class ToggleView extends Backbone.View
events:
@@ -6,7 +7,7 @@ module.exports = class ToggleView extends Backbone.View
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?
@@ -14,10 +15,24 @@ module.exports = class ToggleView extends Backbone.View
@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
Please sign in to comment.
Something went wrong with that request. Please try again.