Giter VIP home page Giter VIP logo

Comments (6)

zourtney avatar zourtney commented on August 21, 2024 9

Angular 1.5+ components can avoid $scope usage by putting cleanup in the $onDestroy hook. Not as concise, but may improve readability.

class MyComponentCtrl {
  unsubscribe;

  /* @ngInject */
  constructor($ngRedux) {
    this.unsubscribe = $ngRedux.connect(...)(this);
  }

  $onDestroy() {
    this.unsubscribe();
  }
}

PS: sorry for posting on a closed issue...it just seemed worth mentioning. It might even be worth adding to the docs.

from ng-redux.

wbuchwalter avatar wbuchwalter commented on August 21, 2024 1

Using $scope in itself is not a bad practice, it's what people do with it that often end up being one.

In the case of ng-redux, $scope is used solely to hook on the $destroy event to unsubscribe from the store.
If you don't want to inject $scope, you could manually unsubscribe in your transient component when it is disposed (for example, when the user click on a close button or something like that), that will work just as fine.

You can totally use ng-redux in a service, and since services are never disposed, you don't need to unsubscribe.

from ng-redux.

orizens avatar orizens commented on August 21, 2024

Thanks.
So, if i use it in a service, all in all, will ng-redux, upon change, run the entire digest loop from root?

from ng-redux.

wbuchwalter avatar wbuchwalter commented on August 21, 2024

Where you use ng-redux has no impact on the digest cycle (ng-redux does not trigger a digest cycle itself), so it won't change anything, it will be angular that will decide wether what you did may have affected the state of the DOM.

Side note: To be exact, the only place where ng-redux may have an effect on the digest loop is in the middleware: All your middlewares are wrapped in a $evalAsync, so that any timeout in one of your custom middleware does not get you out of the angular world, but this has no impact on what we are discussing here.

(I'm closing this issue, since it is not really one, but feel free to continue the discussion)

from ng-redux.

orizens avatar orizens commented on August 21, 2024

Great.
Thanks.
I'm looking into adding ng-redux to the style guide
https://github.com/orizens/angular-es2015-styleguide

from ng-redux.

punmechanic avatar punmechanic commented on August 21, 2024

@zourtney you could use $onInit() instead of the constructor here; although it does mean storing a reference to $ngRedux, it ensures that the connection is only made when the component is ready.

from ng-redux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.