Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOM manipulation in responseInterceptor #1

Closed
Iristyle opened this issue Mar 21, 2013 · 3 comments
Closed

DOM manipulation in responseInterceptor #1

Iristyle opened this issue Mar 21, 2013 · 3 comments

Comments

@Iristyle
Copy link

Thanks for the article.. was going to leave a comment there, but saw the link to the GitHub code.

I would recommend that you rework the DOM manipulation in your responseInterceptor code here:
https://github.com/lavinjj/angularjs-spinner/blob/master/app/app/app.js#L30

As a general rule, Angular code should only perform DOM work inside of directives.

Instead of hard-coding the element in there, you can broadcast an event like this:

$rootScope.$broadcast('event:httpRequestStarted');

$rootScope.$broadcast('event:httpRequestCompleted');

Then inside of the controller you can set a property on the $scope based on the event.

// default to showing
$scope.loadingVisible = true;

$scope.$on('event:httpRequestStarted', function() {
  $scope.loadingVisible = true;
});

$scope.$on('event:httpRequestCompleted', function() {
  $scope.loadingVisible = true;
});

In the markup, you can use the ngShow directive on your element
https://github.com/lavinjj/angularjs-spinner/blob/master/app/index.html#L25

So you would no longer need an id, and your markup would use your scope value to toggle its visibility:

 <div ngShow="loadingVisible" class="row-fluid ui-corner-all" style="padding: 0 .7em; display: none;">

Thanks for the inspiration!

@lavinjj
Copy link
Owner

lavinjj commented Apr 11, 2013

You are completely correct. I originally wrote this back when I was first starting out and didn't really grasp everything at the time. I'll spend some time updating the code to follow best practices.

Thanks!

@Iristyle
Copy link
Author

No prob -- thanks again for the article / contributions to the Angular community!

@lavinjj
Copy link
Owner

lavinjj commented Apr 25, 2013

I've updated the code and this issue has been resolved.

@lavinjj lavinjj closed this as completed Apr 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants