yazpid
yazpid

Reputation: 135

Simple service based on Observable with Angular 1.5

I would like to use Rxjs in my project which is based on Angular 1.5 and written in ECMA 5. I would like to rewrite my service methods which are based on promises and want implement Observables.

I write sth like this in my service:

    function service(Restangular) {

    var self = this;

    self.user = new Rx.Subject();

    var service = {
        artistStream: artistStream,
    };



    function artistStream(artist) {
        self.user.onNext({
            data : Restangular
                    .all('text/')
                    .customGET('search', {q : artist})
        });
    }


    return service;
}

And then I tried use this service in my component like this :

        function testRx(artist) {

            console.log(artistService.artistStream(artist));

         }

I get error from console :

TypeError: self.user.onNext is not a function

What I'm doing wrong? It is my first time when I use Observables

Upvotes: 0

Views: 594

Answers (1)

yazpid
yazpid

Reputation: 135

Ok, rewrite service and work properly.

        function artistStream(artist) {

        const promise = Restangular
                    .all('text/')
                    .customGET('search', {q : artist})

        const subscription = Rx.Observable.fromPromise(promise);

        return subscription;

    }

Upvotes: 3

Related Questions