events ( node ) {
let thisclass = this;
node.on('mouseover', ( d, i ) => {
thisclass.animateParentChain( d );
});
node.on('mouseout', (d, i) => {
d3.select(this).select("circle").attr("r", 2.5);
thisclass.unAnimateParentChain( d );
});
node.on('click', (nd, i) => {
thisclass.persistsAnimateParentChain( nd );
thisclass.loadModal(nd);
});
}
The above is a method in an es6 class. The original script looked like this:
events: function ( node ) {
node.on('mouseover', function ( d, i ) {
app.radialD3.animateParentChain( d );
});
node.on('mouseout', function(d, i){
d3.select(this).select("circle").attr("r", 2.5);
app.radialD3.unAnimateParentChain( d );
});
node.on('click', function(nd, i){
app.radialD3.persistsAnimateParentChain( nd );
app.radialD3.loadModal(nd);
});
},
The new es6 method can be rewritten using the function keyword eg:
events ( node ) {
let thisclass = this;
node.on('mouseover', function ( d, i ) {
thisclass.animateParentChain( d );
});
node.on('mouseout', function (d, i) {
d3.select(this).select("circle").attr("r", 2.5);
thisclass.unAnimateParentChain( d );
});
node.on('click', function(nd, i){
thisclass.persistsAnimateParentChain( nd );
thisclass.loadModal(nd);
});
}
Changing to use the function keyword and things work. Without, the script has issues with this keyword.
I am reasonably new to the es6 stuff, but what is the "proper" way of writing this. In the callback i need this to be the html element and not the class, but at the same time the callback needs to access methods of the class. Using the function keyword seems to be taking a step backward, as does setting the current class into a variable :/
How do you differentiate between the this's ?