Coffeescript重构为dsl

时间:2013-10-04 09:23:27

标签: coffeescript refactoring

如何重构我的代码?

$(document).on "change", '#q_breed_id_eq', ->
  window.filter(this)

$(document).on "click", '#q_city_id_eq', ->
  window.filter(this)

$(document).on "click", '#q_category_id_eq', ->
  window.filter(this)

$(document).on "click", '[name="q[sex_eq]"]', ->
  window.filter(this)

1 个答案:

答案 0 :(得分:1)

这是怎么回事?

register_event = (event, selector) ->
    $(document).on event, selector ->
        #not sure to which "this" you refer
        #it might be necessary to use => or give the
        #reference when calling it
        window.filter @

register_event "change", "q_breed_id_eq"  
register_event "click", "q_city_id_eq"  
...