Issue with accessing variables from within $(window).resize function in coffeescript class

1k views Asked by At

I found a million guides on how to make responsive d3 graphs but none seem to address the problem I'm having. I have a d3 graph being created from a angularjs controller that is a coffeescript class. All of the functions in this class use @width, @height, @margin, @x, @y etc. I need to write a resize function that can access these variables. However, the @ (this) in the resize function points to window, and not the object. I can't seem to figure out a way to have resize act in the same scope as everything else. Any ideas?

EDIT: relevant example code

class Graph
  init: () ->
    @width = 100
  $(window).resize ->
    console.log(@width)  #returns undefined
1

There are 1 answers

0
james emanon On

Not sure this is what you need, but you can pass in the reference as the first arg in the "resize" method.

class Graph
  init: () ->
  w = @width = 100
  $(window).resize w, ->
    console.log(w)