Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting methods that call other methods inside d3 ember components?
    text
    copied!<p>Let's say I am creating a reusable component in Ember, and I want a helper function that calls another helper function defined within. For example,</p> <pre><code>App.SomeCoolComponent = Ember.Component.extend offset: 50 position: (x) -&gt; x * 100 offsetPosition: # would like (x) -&gt; position(x) + offset </code></pre> <p>So conceptually, return a function that would evaluate the position at x, add offset, and return value. Obviously this is a silly example and I could just write <code>offSetPosition</code> without calling <code>position</code>, but in a more complex scenario that is repeating code. The problem is I can't figure out how to get this to work. I tried </p> <pre><code> offsetPosition: (x) -&gt; @get('position')(x) + @get('offset') </code></pre> <p>which fails because <code>@get</code> isn't defined within the function, it has the wrong scope. I've tried to insert things like <code>Ember.computed</code> in various places also with no luck, e.g. the following also doesn't work:</p> <pre><code> offsetPosition: Ember.computed(-&gt; (x) -&gt; @get('position')(x) + @get('offset')).property('position', 'offset') </code></pre> <p>What is the correct way of doing this?</p> <p>Ember version 1.3.0-beta.1+canary.48513b24. Thanks in advance!</p> <p><strong>Edit: seems like my problem stems from passing the function into a d3 call. For example:</strong></p> <pre><code>App.SomeCoolComponent = Ember.Component.extend offset: 50 position: (d, i) -&gt; i * 100 offsetPosition: (d, i) -&gt; @position(d, i) + @get('offset') # Some other code not shown didInsertElement: -&gt; data = [1, 2, 3] i = 1 d = data[i] console.log(@position(d, i)) # works console.log(@offsetPosition(d, i)) # works d3.select('svg').selectAll('circle').data(data).enter().append('circle') .attr('cx', @position) # works .attr('cy', @offsetPosition) # fails .attr('r', 30) </code></pre> <p>The error message is <code>Uncaught TypeError: Object #&lt;SVGCircleElement&gt; has no method 'position'</code></p> <p>Any thoughts on how to resolve this issue?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload