Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created October 18, 2012 07:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mizchi/3910284 to your computer and use it in GitHub Desktop.
Save mizchi/3910284 to your computer and use it in GitHub Desktop.
class Tab
constructor: ->
@$tabs = ($ '.tab')
@$sections = ($ '.section')
@_idx = 0
current_tab: =>
@$tabs.eq(@_idx)
current_section: =>
$tab = @current_tab()
type = $tab.data 'type'
@$sections.filter ".#{type}"
select: (@_idx) =>
@render()
delegate: =>
@$tabs.off()
@$tabs.each (idx, el) =>
($ el).on 'click', 'a', (event) =>
event.preventDefault()
@select idx
render: =>
$tab = @current_tab()
$section = @current_section()
@$tabs.removeClass 'selected'
$tab.addClass 'selected'
@$sections.removeClass 'selected'
$section.addClass 'selected'
$ =>
tab = new Tab
tab.select(0)
tab.delegate()
@r7kamura
Copy link

えっ、select: (@_idx) で@_idxに引数入るの、すごい

@r7kamura
Copy link

@_idxを持ってるならdata-type使わずに@$sections.eq(@_idx)で良いすね

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment