Skip to content

Instantly share code, notes, and snippets.

@kossnocorp
Created December 21, 2012 19:00
Show Gist options
  • Save kossnocorp/4354995 to your computer and use it in GitHub Desktop.
Save kossnocorp/4354995 to your computer and use it in GitHub Desktop.
Classical JS pitfall
class Amplifr.BaseView extends Backbone.View
constructor: ->
@createSelectors()
super
els: {}
createSelectors: ->
for name, selector of @els
cachedMatches = selector.match(/^cached (.+)/)
@['$' + name] = =>
if cachedMatches
@['$_' + name] ||= @$(cachedMatches[1])
else
@$(selector)
@whitequark
Copy link

@itrelease, просто javascript вообще не нужен. В топку его!

@itrelease
Copy link

@whitequark программирование говно!

@kossnocorp
Copy link
Author

@itrelease ага, cachedMatches будет из последнего вызова.

  createSelectors: ->
    for name, selector of @els
      do (name, selector) =>
        cachedMatches = selector.match(/^cached (.+)/)
        @['$' + name] = =>
          if cachedMatches
            @['$_' + name] ||= @$(cachedMatches[1])
          else
            @$(selector)

@kossnocorp
Copy link
Author

@milushov примерно вот так:

  els:
    new:   '@new'
    link:  '@link'
    tabs:  '@tab'
    input: 'cached @input'

@new, @link это алиас на [role='new'].

@kossnocorp
Copy link
Author

@whitequark @itrelease и компьютеры отстой.

@inossidabile
Copy link

for is probably the worst part of Coffee

@kossnocorp
Copy link
Author

@inossidabile maybe worst part of JS?

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