Reactive-Extensions / rx.angular.js

AngularJS Bindings for RxJS
Other
827 stars 89 forks source link

Working with ControllerAs #109

Open agzam opened 8 years ago

agzam commented 8 years ago

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?

SmoshySmosh commented 8 years ago

+1

I'm wondering the same thing.

agzam commented 8 years ago

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

nguyenthong commented 8 years ago

+1

SmoshySmosh commented 8 years ago

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

agzam commented 8 years ago

@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.

SmoshySmosh commented 8 years ago

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?

PhiLhoSoft commented 8 years ago

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).

sirbarrence commented 8 years ago

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?

Roaders commented 8 years ago

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.

jmachnik commented 8 years ago

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?

Roaders commented 8 years ago

SafeApply can be used for noNext, error and complete:

https://github.com/Reactive-Extensions/rx.angular.js/blob/master/src/safeApply.js