Angular js Best Practices presentation notes - misko


Youtube here

Useful best practices and insights especially 9 and 10

angularjs team believes in "configuration over construction" like RoR

"don't fight the html, extend it" - augment the html vocabulary using directives, html as a DSL which you can extend

  1. Use a bootstrap project. angular/angular-seed on github exists to bootstrap an app (you copy and run with it) BUT yeoman.io is recommended now
  2. script files just before the body
  3. angulars modules configure the injector and how objects are created; requirejs deals with how scripts tags are loaded into the browser only done once
  4. If you use something like require js then you must manually bootstrap angular e.g. angular.bootstrap()
  5. wrap callbacks for 3rd party api into $apply
  6. flash of unstyled content momentarily; caused because browser renders before angular. Options:
    1. ng-cloak on <body>   but this will show nothing until angular runs
    2. a better options is to use ng-bind on the index.html only (just first page load since thats only where this could happen) and minimally since you want as much as the page to appear as soon as possible
  7. use proper annotation for fn and params when using minification because DI uses reflection
  8. don't fight the html, extend it
    1. 4 ways to include directives on your page
    2. prefix your directives e.g. myapp-component
    3. if supporting IE8 and below then use <div myapp-component>
    4. optionally can be valid with <div data-myapp-component>
  9. separate presentation from business logic
    1. easier to understand a controller since no rendering 
    2. easier to test
    3. do not do DOM manipulation in Controllers
    4. Controllers are controlled with "what should happen if user does X", "where do I get X"
    5. every time you navigate to view you get a new instance of the Controllers; they're per view
    6. Services are singletons; good use case if you need to share across controllers OR for the lifecycle of the app. Services are more concerned with "Do operation X"
  10. Scope
    1. View scope should be read only; only reading from scope models, not writing
    2. Controller scope is write only; only writing into the scope models
    3. the purpose of scope is to refer to the model, not to be the model; you create the model classes and put them into the scope e.g. if you have a form you should have a form model with properties on it for the inputs
    4. scope references the model; should always have a dot in ng-model; if you don't have a dot then you're doing it wrong; (aka don't bind directly to scope properties)
    5. ...but I'd note you can put functions as well as models in $scope
  11. $watch getter function must be fast because called often; must always return the same result
  12. modules
    1. one module per app
    2. prefer to group by feature because controllers, services, views are often related e.g. login and registration, checkout etc.
    3. or prefer to breakup app by view (in future may be lazy loaded)
  13. performance
    1. develop with non minified code; deploy with minified
    2. use gzip
    3. make index.html non cacheable
    4. cache by version: js code, css, html

More from others




Angular waits for DOMContentReady event and then looks for ngApp. 


Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more