Comments (1)
I'm not comfortable with this integration of RxJS in Vue, I depend on RxJS far to much for toying around. Automatic subscription behind the scenes is magical and limiting and leading to untested errors such as these. I think I'm just going to use the RxJS API where and when I want.
I always liked this approach where everything is in the open:
import Vue from 'vue';
import {Observable, Subject} from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/delay';
import axios from 'axios';
// This function is a typical Observable.fromEvent
// type of thing. Though specific to Vue JS and making state changes
function createObservable(cb) {
let state = null;
const subject = new Subject();
cb(subject).subscribe(function (change) {
change(state);
});
return function (arg) {
state = this;
subject.next(arg);
}
}
new Vue({
el: '#app',
data: {
posts: [],
isLoading: false,
error: null
},
methods: {
// Instead of normal methods we make them expose an observable instead.
// The returned observable(s) has to map to a function that receives the state
// that can be changed. Pretty cool!
fetchClick: createObservable((observable) => {
const getPosts$ = observable
.flatMap(() => Observable.fromPromise(axios.get('http://jsonplaceholder.typicode.com/posts')))
.map(result => result.data)
.share()
const resetPosts$ = observable.map(() => state => state.posts = []);
const startFetching$ = observable.map(() => state => state.isLoading = true);
const stopFetching$ = getPosts$.map(() => state => state.isLoading = false);
const setNewPosts$ = getPosts$
.map(posts => state => state.posts = posts)
.catch(err => state => state.error = err.message);
return Observable.merge(
resetPosts$,
setNewPosts$,
startFetching$,
stopFetching$
);
})
}
})
from vue-rx.
Related Issues (20)
- Vue-Rx prevents passing props as initial value to data
- v-stream data is not being updated in componentInstance HOT 2
- Can't use `subscriptions` as an object. HOT 2
- [help wanted] using vue-rx in component package/plugin HOT 3
- vue renderes before observable HOT 1
- $fromDOMEvent 问题
- Vue Composition API and vue-rx HOT 12
- Created hook called multiple times HOT 4
- How do we use this library with vue3 alpha 8? HOT 2
- help me. $watchAsObservable How to execute directly once the page is loaded HOT 1
- Why have a $subscribeTo ? HOT 1
- Typings error for $observables
- How to use vue-rx with vue 3.x ? HOT 2
- using $watchAsObservable, and using throw new Error() watch not work HOT 1
- Is there any support for Vue 3? HOT 1
- Update to Rx.js 7 and adding Typescript? HOT 2
- Cannot resolve dependency in newer npm version HOT 2
- Publish v6.3.0 in npm
- How to init vuerx to vue3 HOT 3
- Remove Vue installation from the readme
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-rx.