My Usecase: I am new to d3/rxjs, and want this functionality.
Want to use d3.on only when I need to manipulate data from events, but for the entire control flow/ajax requests and stuff, would like Rxjs to come forward, and want to do this often, with as little boilerplate as possible.
MonkeyPatch:
As noppa has already mentioned, Rx.Observable.fromEvent expects a NodeList, which is the standard output of any getElementBy*/querySelector*.
But d3.select/selectAll returns Array[Array]
The exact expectation is in this Rxjs code section. This only expects an [object NodeList], and as Arrays & NodeList already have .length in common, this will work for Rxjs fromEvent.
A monkeypatch is required here because gecko/v8 don't expose a method to make custom NodeList Objects.
Adding to noppa's solutions:
Solution 3
function returnNodeListFrom(selected){
if(Array.isArray(selected)){
newArray=selected;
newArray.toString=function(){return '[object NodeList]'};
return newArray;
}else return selected;
}
Then, you can do
Rx.Observable.fromEvent(returnNodeListFrom(selection[0]),'click').subscribe();
Solution 4
//Slightly better version of 3 using lodash
function returnNodeListFrom(selected){
if(Array.isArray(selected)){
newArray=_.flatten(selected);
newArray.toString=function(){return '[object NodeList]'};
return newArray;
}else return selected;
}
Then, you can do
Rx.Observable.fromEvent(returnNodeListFrom(selection),'click').subscribe();