I am taking a d3 project from here https://codepen.io/meerkat00/pen/LQNYrv and converting it to a signle file component VueJS project. Most of it works except for the click handler. Not sure how to go about it.
In the display component method it sets up the click handler for the children/rectangles:
export default {
name: 'TreeMapDrillDown',
components: {d3, Vue},
props: {},
data: function () {
return {
width: 960,
height: 600,
margin: {top: 24, right: 0, bottom: 0, left: 0},
treemap: null,
transitioning: false,
uscg_jsonData : jsonData,
formatNumber: d3.format(",d"),
svg: null,
grandparent: null,
thisComponent: null
}
},
methods: {
display(d)
{
var self = this;
self.grandparent.datum(d.parent)
.on("click", transition(d))
.select("text")
.text(this.name(d));
var g1 = self.svg.insert("g", ".grandparent")
.datum(d)
.attr("class", "depth");
var g = g1.selectAll("g").data(d._children).enter().append("g");
g.filter(function(d){ return d._children})
.classed("children", true)
.on("click", transition(d));
function transition(d)
{
var self = this;
if (self.transitioning || !d) return;
self.transitioning = true;
}
},
name(d){},
}
The problem is when the object is clicked, and the transition method is called, the scope is for the object clicked - not the vue instance. Using the
self = this;
technique doesn't work because self is no longer defined at this point. How does one access the vue instance to get to member variables and/or methods?
I tried in the click handler to pass a this into it, but self ends up being undefined, so that didn't work.
display(d)
{
self = this;
g.filter(function(d){return d._children})
.classed("children", true)
.on("click", transition(d, self));
}