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
Post a Comment