javascript - How can I run a coffeescript method that depends on the js event while maintaining DRY code? -


i want dry code:

@$canvas.on 'mousemove', (e) =>   return unless @running   @mousetarget.set @board.x + e.clientx, @board.y + e.clienty * 2   @player?.mousemove()  @$canvas.on 'mousedown', (e) =>   return unless @running   @mousetarget.set @board.x + e.clientx, @board.y + e.clienty * 2   @player?.mousedown()  @$canvas.on 'mouseup', (e) =>   return unless @running   @mousetarget.set @board.x + e.clientx, @board.y + e.clienty * 2   @player?.mouseup() 

i want having effect of:

@$canvas.on 'mousemove', 'mousedown', 'mouseup' -> @mouseaction  mouseaction: (e) =>   return unless @running   @mousetarget.set @board.x + e.clientx, @board.y + e.clienty * 2   @player?.mouseup() # here problem... 

the thing is, how alternate between @player?.mouseup(), @player?.mousedown() , @player?.mousemove() while maintaining dry code?

to expand on pdoherty926's answer, want somehow same thing, invoke different methods on @player. if control method names , can use event type, can pdoherty926 suggests -- otherwise, here's suggestion more explicit solution:

@$canvas.on 'mousemove', @mouseaction 'mousemove' @$canvas.on 'mousedown', @mouseaction 'mousedown' @$canvas.on 'mouseup', @mouseaction 'mouseup'  mouseaction: (action) =>   (e) =>    return unless @running    @mousetarget.set @board.x + e.clientx, @board.y + e.clienty * 2    @player?[action]() 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -