Note that there are some explanatory texts on larger screens.

plurals
  1. POd3.js force layout namespace in a backbone view
    primarykey
    data
    text
    <p>I'm working on a medium-complex app using backbone.js to handle wordpress data, and i can't figure out how to get the force working in a backbone layout.</p> <p>basically, i'm trying to instantiate a force layout within a backbone boilerplate layout, like this:</p> <pre><code>myLayout = Backbone.Layout.extend({ initialize: function() { var f = this; // i.e. the layout instance f.force = d3.layout.force() .nodes(myModels) .on("tick", f.tick) .gravity(0) .friction(0.9) .start(); console.log(f.force); }, tick: function() { // stuff to do when the force ticks } }); </code></pre> <p>The problem is that the force is being defined with all blank functions, like <code>gravity: function(x) { //lots of null things here }</code>. i'm pretty sure it's a namespacing issue, but nothing i try works - i've tried doing <code>$(window).force</code>, <code>var force</code>, <code>$this.force</code>...</p> <p>in my example <code>tick</code> is the only namespaced function, but i've tried doing that with all the others too (gravity, friction, etc.) to no avail (even though they should just be chaining onto the force object).</p> <p>anyone have any ideas? i can't really post a .jsfiddle because the app is too complicated, so sorry in advance about that. The current version is up <a href="http://learning.samgalison.com/wp_bb/" rel="nofollow">here</a></p> <p><strong>edit</strong>: here's how d3 can access the models successfully:</p> <p>this works:</p> <pre><code>myLayout.nodes = myLayout.d3_wrapper.selectAll(".node") .data(myModels) .enter().append("g").attr("class", "node") .attr("x",10) .attr("y",10); myLayout.nodes.append("clipPath") .attr("id", function(d) { return d.get("slug"); }) </code></pre> <p>as does this: <code>myLayout.nodes.append("clipPath") .attr("id", function(d) { return d.attributes.slug });</code></p> <p><strong>edit</strong>: in the interest of clarity, here's the non-nicknamed code:</p> <pre><code>setforce: function() { // this gets called from the layout's initialize fn console.log("setting force"); var f = this; // the layout f.force = d3.layout.force() .nodes(Cartofolio.elders.models) // Cartofolio is the module, elders is a Backbone Collection .gravity(0) .friction(0.9) .start(); console.log(f.force); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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