Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Working with ControllerAs #109

Open
agzam opened this issue Oct 26, 2015 · 11 comments
Open

Working with ControllerAs #109

agzam opened this issue Oct 26, 2015 · 11 comments

Comments

@agzam
Copy link

agzam commented Oct 26, 2015

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
Copy link

+1

I'm wondering the same thing.

@agzam
Copy link
Author

agzam commented Nov 16, 2015

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
Copy link

+1

@SmoshySmosh
Copy link

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

@agzam
Copy link
Author

agzam commented Nov 17, 2015

@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
Copy link

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
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

Roaders commented Jan 26, 2016

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
Copy link

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
Copy link
Contributor

Roaders commented Mar 17, 2016

SafeApply can be used for noNext, error and complete:

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants