Reactive-Extensions/rx.angular.js

Working with ControllerAs

Opened this issue · 11 comments

agzam commented

Can someone please explain how to use it with controllerAs

I know that you can watch on the controller using observeOnScope, also you can create streams out of ng events like this:

 rx.createObservableFunction(self, 'keypress')

But then how can I use rx to $watch and do events without having to inject both rx and observeOnScope?

+1

I'm wondering the same thing.

agzam commented

apparently Rx adds a a few new things on $scope so you can do something like this:

$scope.$toObservable('ctrl.myProp', true).subscribe((x)=> console.log(x));

one thing I can't figure out still, have to turn $rootScope.$on events into observables

I think the point here is to not use watchers with scope.

agzam commented

@SmoshySmosh I'm not sure I get what you're saying? I just wanted to know how to create streams out of $watchable things without having to inject stuff like observeOnScope into the controller.

I could be wrong on this, but I believe that the topic was about using the controllerAs syntax so you can avoid the use of $scope. It would be nice to use rx to actually observe objects without the use of observeOnScope (which makes a watcher). Maybe something like observeOnVm?

Personally, even before using Rx, I rarely, if ever, use $watch in controllers. I find more effective to use ngChange, ngClick, etc.
Now, with this project, I use something like: rx.createObservableFunction(vm, 'showDialog') { ... }); where vm contains this.
safeApply still need the injected $scope, though.
Idem for observeOnScope:
observeOnScope($scope, function() { return vm.userInput; })...
I kept it here for experimentation, on an input field where user types something to search for (the Wikipedia example). As you see, I used the function form, allowing to select what variable to observe via the vm syntax, avoiding to use a reference to the "as" name (ie. the name used in the template).

one thing I can't figure out still, have to turn $rootScope.$on events into observables

to turn an Angular event into an Observable, use $rootScope.$eventToObservable(eventName).

Does that answer all your questions @agzam?

I think the point is similar to my issue #123 . I used to use ScopeSchedular to do an apply whenever an observable pushed a message but this has now gone. Using ScopeSchedular I didn't have to inject a scope into my controllers at all and apply got called whenever I needed it to without me having to do anything within my controllers.
I now have to inject scopes into my controllers and manually link up safeApply on each observable I use.
I wonder why scopeschedular was removed and why we can't have a similar solution now.

I now have to inject scopes into my controllers and manually link up safeApply on each observable I use.

But it does not fixing issue with notyfing angular to update scope after onError, does it?