Cordova resume application and angular $apply() on PhoneGap app - angularjs

I have Cordova/Ionic/AngularJS application I am currently testing using the PhoneGap app on iOS. This application uses WebSockets to receive external inbound messages. An Angular service function pushes the new messages in an array and binds the array to a scope. I then use $rootScope.$apply() to propagate the change to my view that displays this array of messages.
Everything works fine except when I pause and resume the application. In that case, any new incoming message is indeed correctly stored in the messages array however the view is not updated as I would expect from $apply(). Leaving the view and returning to it fixes the problem. It's as if reloading the view's controller and calling the service resets something.

Related

Updating $rootScope of hosting app in widget

Using angular-widget we are hosting a lazily loaded angularjs app. I've created a service in the hosting application to expose a loading screen, that widgets can use to tell the hosting app to show/hide the loading screen.
The loading screen is triggered by setting Loading on $rootScope to true. The service simply exposes a show() and hide() method and keeps track of who asks for the loading screen, in case of multiple things asking for a loading screen at the same time.
When triggering the service in the hosting application, it works just fine. However, triggering the same service in the hosted application, it seems that the update to $rootScope.Loading happens, but the view in the hosting app doesn't update. I know the update is happening, because I put a $watch on $rootScope.Loading which does trigger, but like I said, the view doesn't update to reflect the change.
I've got a plunker here. Am I missing something?
Turns out I just needed to add widgetsProvider.addServiceToShare to my config. Once that was added, the view started updating as expected.

AngularJS/Ionic how to initialize a controller's code without rendering the view?

I am building an app where I have a number of controllers. I'm watching for a web socket message in my chatController which is connected to a inbox.html view. The app starts on the 'requests' page where the controller is different. The problem is unless the user has explicitly visited the inbox.html view at least once, the web socket(Socket.io) event isn't detected since the code for the 'chatController' hasn't been initialised. What options do I have for enabling this functionality? I would like to somehow initialise the code for the 'chatController' as soon as my app starts without the user having to visit the screen.
Thank you

Why does my angular js service gets destroyed after server call?

As per my understanding services in angularjs are "Singleton". Following is my Scenario where I am using angularjs with Asp.net MVC application.
I have created angularjs service and I use it in one of my controller, which is used on one of the HTML view. If I navigate from that page to some other page, Which results in call to Server then in that case my angularjs Service gets reset. Later, When I come back to the same page my service again gets created/initialized. Can someone please explain me the reason?
Most likely you use ngRoute or angular-ui routing for making single page app, when you change pages controller gets reloaded, which forces all the logic in controller to run again hence call your service, If you are on the view which use other controller its expected not to run service from first one. Some code would be nice too.
hope this helps.

Why is my UI updated only once?

I have done a simple SPA with AngularJS/signalR that send a notification to my hub Hello when the app starts.
On the client side I have list of notifications managed by a controller. This list got updated only once whereas I have been able to see that my callback is called every time thanks to the console.log (and the debugger shows that this.messages grows at every notification received).
I don't get why the UI only update on first call (which the one the current client have emittted)
Here is the code that work only once.
NotificationCtrl.prototype.hello = function() {
console.log("hello");
this.messages.push(new Notification("Tom", "Now", "Is now connected"));
}
You need to $scope.$apply because the callback is not scope aware since its not called from angular.
I think you would benefit alot from my library called SignalR.EventAggregatorProxy
Its designed around the Event aggregation pattern and is perfect for MV* enabled sites with Knockout or Angular.
Have a look at the wiki for setting it up
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki
Demo project
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/master/SignalR.EventAggregatorProxy.Demo.MVC4
Recent blog post I did about it
http://andersmalmgren.com/2014/05/27/client-server-event-aggregation-with-signalr/
Install using nuget
Install-Package SignalR.EventAggregatorProxy

IBM Worklight 6.1 - Challenge Handler inside Angularjs controller

I am using adapter-based authentication that I am trying to implement using AngularJS. I have a single realm that protects the application, and all procedures. I've defined a Login Controller that handles authentication related activities:
Note: Testing with MBS on Chrome and Worklight Development Server
$scope.sampleAppRealmChallengeHandler = WL.Client.createChallengeHandler("LoginBluePageRealm");
$scope.sampleAppRealmChallengeHandler.isCustomResponse = $scope.isCustomResponse;
$scope.sampleAppRealmChallengeHandler.handleChallenge = $scope.handleChallenge;
The Login view is the initial view displayed due to issuing WL.Client.connect(), and authentication works fine collecting credentials, and transitioning from the login view to the main menu view.
I cause the connection to the server to fail (by restarting the WL dev server), and then select an activity in the application that will invoke a protected adapter call. I see in the JavaScript console that $scope.isCustomResponse() is reached, but then there is no entry into the $scope.handleChallenge();.
Is there any issues with defining the challenge support in $scope, or is there a requirement that they be defined at the $rootScope level.
Appreciate any advice defining authentication within AngularJS.
Can you add logs to isCustomResponse to actually see responseText/responseJson property of what it's getting? Also - before returning true/false from isCustomResponse log your response. The handleChallenge function is not invoked in case isCustomResponse returns false, so you might be missing some if.
In general you should not define any javascript object that you would like to persist in Angular via $scope. Angular can destroy $scope variables at any time. Instead, wrap the challenge handler in an Angular service/factory definition since these are singletons that persist. This is a good video I would suggest watching for Worklight + Angular. https://www.youtube.com/watch?v=a89W_atlhjg

Resources