Angular spinning directive gets instantiated after controller load - javascript

I have a controller. Here is the relevant part of the constructor function (what is the correct term for this function?):
activate();
function activate() {
$scope.$broadcast('ctrlLoadingStarted');
var promises = [getUsers()];
return $q.all(promises).then(function (eventArgs) {
$scope.$broadcast('ctrlLoadingFinished');
});
}
So the activate function is basically a generic function that takes in an array of data getting functions. It broadcasts on start and broadcasts on finish.
Then I have a directive nested in the controllers scope, here is the relevant part of the directive:
function link(scope, element, attrs) {
scope.$on('ctrlLoadingStarted', function (event, args) {
scope.spinnerOn = true;
});
scope.$on('ctrlLoadingFinished', function (event, args) {
scope.spinnerOn = false;
});
}
As you can see, it is simply listening to the start and finish events and turning a spinner on and off.
The issue is that the controller seems to be instantiating before the directive. This results in the broadcast going off and not turning the spinner on.
I tried to simply put scope.spinnerOn = true; in the first line of the directive (this way, no matter when the controller instantiates relative to the directive instantiation, the spinner will always go on). However, the problem I ran into there was that the controller's second broadcast would go out before the directive was instantiated as well causing an endless spinner.
I just want to ensure that if the controller instantiates before the directive but then the data-getting takes a while that the spinner will spin.
All advice is appreciated, thanks in advance.

Trigger the activate function in $timeout.
function activate() {
$scope.$broadcast('ctrlLoadingStarted');
var promises = [getUsers()];
return $q.all(promises).then(function (eventArgs) {
$scope.$broadcast('ctrlLoadingFinished');
});
}
$timeout(activate)
With this the activate function will be called in next digest cycle.
More about Digest cycle Integration with the browser event loop section at: https://docs.angularjs.org/guide/scope
Disclaimer: This is my understanding of how Javascript / browser works. It may not be correct.

Related

ng-repeat with directive: how to destroy repeated items after array has changed?

I have an angular js app, that uses an ng-repeat with a directive like this:
<div data-ng-repeat="n in items">
<div data-my-directive
item="n"></div>
</div>
where items is an array with integers.
Depending on actions of the user, the items array can be completely destroyed and made anew with new integers.
First time, it may be [1,2,4,9]
and next it may be [1,3,6,7]
for instance. This is dependent on some user choices.
The directive my-directive will perform some business logic server-side, so it will call the server as soon as it gets loaded. And then after a result returns, it shows a nice table to the users.
The problem is that some users don't wait until everything is nice and loaded and switch their view (which means the array changes). In this case, I see that the calls to the server are still being executed and that the result-function is still being called, even though the directive itself has been destroyed because the ngRepeat has rebound and all of the directives are re-made.
For instance:
$http.get(service.url + '/filters').success(function(result) {
alert(result);
});
This will display all of the alerts, even of the directives that are no longer on the page.
This poses a few problems. Can I destroy the directives in the repeat when the array changes or something like that to make sure that no logic is executed in a directive that shouldn't exist anymore (or that isn't displayed on the page anymore) ?
Or do you have some other ideas on how best to approach this?
Ideally, the directives should just disappear after the ng-repeat has rebound itself, so no logic is executed as soon as data comes back from the server.
When the user changes the parameters you can cancel the running request and start a new one.
In this Scott Allen's blog post you can find the detailed explanation of how this work.
You start creating a service or a factory with the method you will call:
var getData = function(){
var canceller = $q.defer();
var cancel = function(reason){
canceller.resolve(reason);
};
var promise =
$http.get(service.url + '/filters', { timeout: canceller.promise})
.then(function(response){
return response.data;
});
return {
promise: promise,
cancel: cancel
};
};
Then you call it in this way:
var request = service.getData();
$scope.requests.push(request);
request.promise.then(function(movie){
$scope.movies.push(movie);
clearRequest(request);
}, function(reason){
console.log(reason);
});
You then provide a method that will cancel the request:
$scope.cancel = function(){
var request = // retrieve the correct request from the requests array
request.cancel("User cancelled");
// Remove the request from the array
};
So I have a few thoughts for your question.
First you could use ng-cloak which is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. I find this very helpful if I want the user to wait until all the data has returned to view the page. ex.
<div id="template1" ng-cloak>{{ 'hello' }}</div>
Second you could try a resolve. A resolve contains one or more promises that must resolve successfully before the route will change. This means you can wait for data to become available before actually changing routes.
$routeProvider
.when("/news", {
templateUrl: "newsView.html",
controller: "newsController",
resolve: {
message: function(messageService){
return messageService.getMessage();
}
}
})
The directive needs to use the $destroy event to cancel operations in progress.
app.directive("myDirective", function() {
return {
controller: function($scope, $http, $q) {
var canceller = $q.defer();
var cancel = function(reason){
canceller.resolve(reason);
};
$http.get(url, { timeout: canceller.promise})
.then(function(response){
$scope.data = response.data;
});
$scope.$on('$destroy', function () {
cancel("Scope destroyed");
});
}
}
});
When the ng-repeat removes an item, it destroys the scope of the item. Use the $destroy event to cancel asynchronous operations in progress.
From the Docs:
The $destroy() method is usually used by directives such as ngRepeat for managing the unrolling of the loop.
Just before a scope is destroyed, a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it a chance to perform any necessary cleanup.
--AngularJS $rootScope.scope API Reference -- $destroy

Does using .on to bind click benefit from ngTouch?

Currently our project does not have ngTouch but it will at a future time. We are still learning Angular as we go.
I have this simple directive
app.directive('myDir', ['$log', function($log) {
return {
restrict: "A",
link: function($scope, iElm, iAttrs, controller) {
iElm.on('click', function($event) {
// functionality...
});
}
};
}]);
I am doing the binding of the click here instead of using ng-click because I don't want the html markup to show.
Will this type of binding take advantage of ng-touch when we include it? By that I mean if you use ng-click with ng-touch included then there will be no more 300ms delay after click/tap. So if I don't use ng-click and just use .on will it still work the same?
No, touchstart is a different event than click. You would need to do:
element.on('click touchstart', function (event) {
/* ... */
});
The above would register a handler for both events at the same time. Also keep in mind that because this event handler is triggered outside of an Angular digest cycle, you need to wrap its contents in $scope.$apply.
app.directive('myDir', function ($log) {
return {
restrict: 'A',
link: function ($scope, element, attrs) {
element.on('click touchstart', function (event) {
$scope.$apply(function () {
// functionality...
});
});
}
};
});
Edit: Re: Angular digest cycle.
When you do things inside of an angular controller or inside of an angular directive, all of that logic is running inside of an angular digest cycle. The digest cycle is basically angular's event loop. As long as angular is the one that launched a logic path then angular is able to ensure that things happen in the right order. When things happen in the digest cycle as expected angular is able to update the UI appropriately and all is well.
This is important to understand when you do things like register an event handler from inside an angular directive or controller. If the event is triggered by something that isn't wrapped in angular magic (like most of the native directives that register DOM event handlers for you behind the scenes) then that logic path is happening outside of an angular digest cycle. The side effect you'll likely see is that even though the code runs and the scope data is modified as expected, the UI will not be updated. The next time that scope variable changes in a digest cycle suddenly you'll see the change that was made.
To fix this you need to manually register the logic you want to perform with angular so it can perform that logic at the appropriate time during a digest cycle, updating the UI as expected.To do this you simply wrap the logic you want to perform inside of $scope.$apply which just takes a simple callback. It then manually begins a digest cycle and calls the callback you supplied during that cycle.
If you're curious, this is literally all the ng-click directive does:
app.directive('ngClick', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var fn = $parse(attrs.ngClick, null, true);
element.on('click', function (event) {
scope.$apply(function () {
fn(scope, { $event: event });
});
});
}
};
});
It uses the built-in $parse utility to parse out the expression you supply to ng-click and execute it. It passes in the local scope to the expression so you can use variables in scope inside your expression. This is why you can do ng-click="myFunction(someScopeVar)". It also passes in a custom variable called $event available only inside that expression. Notice that it's just literally the JQuery wrapped DOM event. Notice also that Angular wraps the DOM event logic in a call to $scope.$apply so that it happens in a digest cycle appropriately.

Merge multiple Angular scope watch statements

I have multiple different watch statements in a directive that watch scope and re-render a d3 directive on different events (window resize, form submit, etc...)
window.onresize = function() {
scope.$apply();
};
// Call render function on window resize
scope.$watch(function() {
return angular.element($window)[0].innerWidth;
}, function() {
scope.render(scope.data);
});
// Watch for data changes and re-render
scope.$watch('data', function() {
return scope.render(scope.data);
}, true);
Problem is, I'm ending up calling render multiple times for the same event. Is there any way I can merge these three different $watches into a more concise structure? I had guessed that watching scope would also watch the data attribute of scope, but apparently not! Why is this?
Might not be the best solution, but it may help.
underscorejs' throttle takes a function and a minimum time between executions of the function and returns a function that will only call the function specified at most once every wait milliseconds.
_.throttle(func, wait, [options])
info

why the need to use 'timeout' in angular

This is probably a total newb question...apologies, but I can't get my head around it.
In a lot of angular documentation/examples I see asynchronous functions wrapped in 'timeout' blocks. Many are wrapped in setTimeout() and require the explicit use of
if (!$scope.$$phase) {
$scope.$apply();
}
Given that angular provides $timeout, the above code just seems outdated or wrong and within angular the use of $timeout should always be preferred. However, I digress.
Here is a snippet of some example code taken from: http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/
var myModule = angular.module('myModule', []);
// From this point on, we'll attach everything to 'myModule'
myModule.factory('HelloWorld', function($timeout) {
var getMessages = function(callback) {
$timeout(function() {
callback(['Hello', 'world!']);
}, 2000);
};
return {
getMessages: getMessages
};
});
I see this wrapping of code in timeout blocks everywhere particularly related to asynchronous calls. But can someone explain why this is needed? Why not just change the code above to:
var myModule = angular.module('myModule', []);
// From this point on, we'll attach everything to 'myModule'
myModule.factory('HelloWorld', function() {
var getMessages = function(callback) {
callback(['Hello', 'world!']);
};
return {
getMessages: getMessages
};
});
Why wouldn't the code snippet above work just fine?
The use of $timeout or $interval is to implicitly trigger a digest cycle. The process is as follows:
Execute each task in the callback function
Call $apply after each task is executed
$apply triggers a digest cycle
An alternative is to inject $rootScope and call $rootScope.$digest() if you are using services that don't trigger a $digest cycle.
Angular uses a dirty-checking digest mechanism to monitor and update values of the scope during
the processing of your application. The digest works by checking all the values that are being
watched against their previous value and running any watch handlers that have been defined for those
values that have changed.
This digest mechanism is triggered by calling $digest on a scope object. Normally you do not need
to trigger a digest manually, because every external action that can trigger changes in your
application, such as mouse events, timeouts or server responses, wrap the Angular application code
in a block of code that will run $digest when the code completes.
References
AngularJS source: intervalSpec.js
AngularJS source: timeoutSpec.js
$q deferred.resolve() works only after $timeout.flush()
AngularJS Documentation for inprog | Digest Phases
The $timeout in your example is probably used just to simulate an async function, like $http.get. As to why $timeout and not setTimeout: $timeout automatically tells angular to update the model, without the need to call $scope.$apply()
Also, consider the following example:
$scope.func = function(){
$scope.showSomeLoadingThing = true;
//Do some long-running stuff
$scope.showSomeLoadingThing = false;
}
No loading thingy will be shown, you would have to write it like this:
$scope.func = function(){
$scope.showSomeLoadingThing = true;
$timeout(function(){
//Do some long-running stuff
$scope.showSomeLoadingThing = false;
});
}

Angular's $rootScope.$digest

Reading this excellent book, Mastering Web Development in AngularJS, I ran across this code:
var Restaurant = function ($q, $rootScope) {
var currentOrder;
this.takeOrder = function (orderedItems) {
currentOrder = {
deferred:$q.defer(),
items:orderedItems
};
return currentOrder.deferred.promise;
};
this.deliverOrder = function() {
currentOrder.deferred.resolve(currentOrder.items);
$rootScope.$digest();
};
this.problemWithOrder = function(reason) {
currentOrder.deferred.reject(reason);
$rootScope.$digest();
};
My understanding is that the $rootScope.$digest(); calls are made in order to alert Angular that the Promise's state has been updated.
Is my understanding correct? Also, is it necessary to make the above $rootScope.$digest(); calls?
$scope.$digest() is what processes all of the $watch events that are on the current and children scope. It essentially manually tells the scope to check if a scope variable has changed. You don't generally want to use this when you are inside of a controller or a directive, because the $scope.$apply() function calls the $digest anyway and it is called when you mutate a scope variable.
Checkout this link for an example.
You don't need a $rootScope.$digest here because resolving/rejecting the promise will fire a $rootScope.$digest internally, as $interval,$timeout,and $http (after request finished) do that for you. And this $digest can throw errors of $digest already in progress.

Categories