Tero Parviainen: Build your own Angular JS

Orde Saunders' avatarPublished: by Orde Saunders

Tero Parviainen (@teropa) was talking about building your own AngularJS at Scotland.js, these are my notes from his talk.

There is my code and everyone else's code.  I can live with  my code - other people's code intimidates me.   I understand my own code, that's not quite as true with other people's code.  With my code I can find a solution by reasoning about it, for other people's code I need to use Google searches.

Might be addressing symptoms rather than the cause?  Might be several things doing the same thing?

Stackoverflow driven development is sub optimal.

Decided to learn Angular from the inside out, to learn a framework try building one yourself then throw it out as you now have the mental model for how the real framework works.

How to ruin JS in 10 minutes

Angular dependency injection.

Pull tightly coupled actions out of objects and make them arguments -> push them in rather than pull them in.  With a small app this isn't a problem but in Angular it's helpful to have a framwork.

You can register components into the framework and then ask them to inject them where they're needed.

In JS you can get the function as a string and match the arguments.  fn.toString().match[/^function\s.... is pretty ugly and there are better ways.  We end up with an array of argument names which we can then lookup in the cache and apply them.

By using a factory we can remove dependency constants.  We need two caches, one for providers and the other for instances - if there is nothing in the instance cache then we will look in the provider cache.  We'll then put that into the instance cache for next time.

It's time consuming to break something down and then build it up again but you learn a lot from doing it.