I'm having a hard time getting flatMap
to work with Rx.DOM.getJson()
some of the time and can't figure out what I'm doing wrong.
html
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs-dom/7.0.3/rx.dom.js"></script>
<script src="https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-xta1/t39.3284-6/12624096_747368668729465_1053913233_n.js"></script>
<script src="https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-xaf1/t39.3284-6/12624065_1518596455100766_427485197_n.js"></script>
<title>JS Bin</title>
</head>
<body>
<button id='fetch'>Fetch</button>
<div id="content"></div>
</body>
</html>
javascript
// Actions
const Actions = {
LoadPosts: 'LoadPosts'
};
// Disptacher
const Dispatcher = new Rx.Subject();
function send(action) {
Dispatcher.onNext({action: action});
}
// Stores
const url = 'http://jsonplaceholder.typicode.com/posts';
const FetchedPosts$ = Dispatcher
.filter(x => Actions.LoadPosts === x.action)
.do(x => console.log(x))
.flatMap(() => {
console.log('flat');
return Rx.DOM.getJSON(url);
})
.do(x => console.log('after', x));
// Views
const fetchButton = document.getElementById('fetch');
const Fetch$ = Rx.Observable.fromEvent(fetchButton, 'click')
.do(x => send(Actions.LoadPosts));
const PostsView$
= FetchedPosts$.map(posts => posts.map(post => <li>{post.title}</li>));
const RootView$ = Rx.Observable.merge(Fetch$, PostsView$);
RootView$.subscribe(
content => ReactDOM.render(
<ul>{content}</ul>,
document.getElementById('content')
)
);
output
[object Object] {
action: "LoadPosts"
}
"flat"
"Script error. (line 0)"
So the output shows that I'm getting into the flat map and if I pull out the Rx.DOM.getJSON(url)
and subscribe to it then I can write out the value that gets returned. However, from in the flatMap nothing returns and I'm seeing an error in the network tab showing that Rx is canceling the request as shown below.
Here's a JsBin: http://jsbin.com/geruzo/edit?js,console,output
Edit I just noticed that removing react allows the Rx.DOM call to succeed.