Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Up Close: Demystifying Angular by Going to the ...

Up Close: Demystifying Angular by Going to the Source

@ ngNYC 11/3/2014

Avatar for Joseph Alves

Joseph Alves

November 04, 2014

Other Decks in Programming

Transcript

  1. Obligatory About Me Slide To Ease You Gently Into the

    Presentation @joedotjs So gentle . . . So a little bit more about me: ! My name is Joe and I have been writing Javascript for about 3 years now. I work at an amazing company downtown called Lawline and for the past 4 months I have been writing a greenfield project in Angular and node.js. And I'm having a great time. ! About a week into it, I had a meeting with my team showing off these amazing things I did with Angular and it felt a lot like this:
  2. Anybody who's written Angular definitely has this feeling at first.

    Our lead developer said, "Wow, this Angular stuff is amazing. You are clearly the coolest guy in this room right now. But how does it work? What is it doing?" ! And I kind of stared at him blankly and said, "Uhhhhh, it's magic?"
  3. Speaker’s note: Linger on this slide a bit for a

    full Shia Labeouf effect. And it does kind of feel like magic sometimes, right? We can do a whole lot with Angular with just a few lines of a code. Throw an ng-model onto an input, couple curly braces, maybe an ng-click or two and we've got something that's actually useful without even thinking about it. ! I was listening to a podcast not too long ago and one of the panelists said along the lines of this:
  4. “We’d be writing an Angular application for about two years

    and started to experience noticeable performance issues. I opened up dev tools and that’s the day I first learned what the $digest loop is.” Really, this just blows my fucking mind. First, two years and you haven't written a single test because you would have encountered the digest loop -- look, nevermind. ! The point is, a very smart, experienced engineer used a framework for 2 years and never encountered what is probably the most crucial part of its internal workings. I don't think this is laziness on the engineer's part, I think it is a feat of engineering on Angular's part.
  5. WRITTEN BY WIZARDS Angular is an extremely intuitive tool, especially

    for beginners. I credit its growing popularity to that characteristic. It was created originally for designers with less Javascript experience to build user interfaces for the web and I think, although it has grown past that, the fruits of that original intent are still there.
  6. TELL US YOUR ! SECRETS Because, Angular just works. And

    building something that just works and feels like magic is nothing less than pure wizardry.
  7. UP CLOSE Demystifying Angular by Going to the Source And

    I am here today to just ruin all of that for you, strip you of all of that wonderment and naive bliss. I am pulling the head off of Mickey Mouse; I am telling you that Santa isn't real, on your birthday. ! And I'm going to do that by showing you tons of Angular source code.
  8. HOLD ON ! TO YOUR BUTTS Speaker’s note: Never read

    off of your slides. So, hold on to your butts.
  9. But let's start simple. ! Here is a really basic

    piece of HTML. We have a button, it has the text "Yes please!", and when we click, we call a function called free puppy. I don't know if this function actually gives you a free puppy, I have not seen the controller.
  10. But the part of this I want to talk about

    is this. We have javascript in our HTML attribute. Wow.
  11. Okay, fine, this is not something new. We've been doing

    able to do this for a long time. ! But these kinds of handlers are extremely limiting and unless you're putting everything on the window object, you are not getting anything done and you WILL make children cry, you will.
  12. So how does Angular know how to execute this string,

    this HTML attribute, as javascript code? I can tell you for a fact it's not eval. That is the start of our anti-magical journey into Angular:
  13. $par Speaker’s note: Leave in the bad kerning to weed

    out the designers. se the $parse service.
  14. Angular Expressions {{ 2 + 2 }} {{ firstName +

    ‘ ‘ + lastName }} {{ tomato || potato }} {{ makeARainbow(‘yay!’) }} puppy in puppies | orderBy:’cuteness’ So, there is a little-known, heavily-used concept of Angular expressions. And your inside voice is screaming at me right now, "Yeah, I know what Javascript is.” ! But actually, when you write these expressions in your HTML, it isn’t *technically* Javascript. Now what do I mean by that? Well, I mean, this is not actually being run as Javascript code. They are being interpreted and executed by Angular.
  15. ANGULAR LIED TO YOU. Angular lied to you. You have

    not been writing Javascript in your HTML. ! It actually has this incredibly large, well-engineered object underneath that makes its own rules about a Javascript-like language. But as consolation it gives you the power of this object via the $parse service.
  16. It’s injectable! $parse #1 So $parse can be injected into

    any injectable Angular function, so your controllers, services, run, whatever. So let's take a look-see at how it works: PLUNK 1
  17. $parse creates the illusion of Javascript. So, Angular's parser pretty

    much casts a spell that creates an illusion of Javascript. That is pretty damn magical. ! And you will never again wonder how Angular lets you put Javascript in your HTML, just like you no longer wonder how presents get under a Christmas tree. And that's because Javascript in your HTML, just like Santa, isn't real. You've learned something that cannot be unlearned, and I have ruined it for you.
  18. If you've used Angular, you've used ng-model. If Angular is

    magic, ng-model is the wand. But did you know that when you use an ng-model you are setting up a weird bidirectional game of telephone?
  19. ng-model $viewValue $modelValue When you "ng-model" something, you are actually

    creating an entity that represents TWO values, a view value and a model value.
  20. ng-model $viewValue $modelValue (in the input) (on the scope) #3

    The view value is in your input, the model value is on your scope. Now, this is not a very well-known characteristic because normally a view value and a model value stay about the same. But let's look at an example: PLUNK 3
  21. $viewValue Yo, I updated! $parsers pipeline $modelValue Word up! So

    when our view value updates, the value gets thrown through a pipeline of parsers that will put it on the scope.
  22. $viewValue I got this! $formatters pipeline $modelValue Yo, I updated!

    And it also works the other way! Except that pipeline is called $formatters.
  23. $setViewValue It's okay if this doesn't quite make sense, because

    what I want to address is how ng-model, how Angular, sets these viewValues. And it is this function called setViewValue, which is available on the ngModel construct.
  24. And what Angular has done is not too mysterious at

    all. It is the same thing we should do in something like jQuery. When this input changes, we set the viewValue.
  25. And when you put ng-model on an element, Angular figures

    out how that viewValue gets set. Like a select, it just registers a listener for the change event.
  26. d #4 Or a checkbox, where it listens for a

    click. ! So there is no mystery here. If you put ng-model on some kind of input element, ng-model will figure its way out to make it so that it talks to your scope. ! But what I think is most amazing is ng-model and setViewValue allow you to make ANYTHING an “input". PLUNK 4
  27. READ THE SOURCE But I don’t wanna DO IT So

    there is a bunch more I would love to talk about, in particular the $digest loop and the $compile service, but unfortunately I'm running out of time. ! So I'd like to leave you with a few tidbits of advice if you want to become a better Angular developer. Read the source. Yeah sometimes it's a little scary, like here:
  28. This is really funny to me actually. Yeah, this code

    looks like incomprehensible shit, but don't worry, there are tests! It's like the new Here Be Dragons, which is fine
  29. Your journey: But I do encourage you to venture into

    the source and here's where I think you can start, especially as a beginner/intermediate Angular-er:
  30. Your journey: Learn how directives work. Google: “Dan Wahlin custom

    directives” Learn how to write custom directives. You don't need to read the source for this. If you don't know how already, learning this will wield Angular power you could only previously imagine.
  31. Your journey: Learn how directives work. Google: “Dan Wahlin custom

    directives” Investigate how Angular implements simpler directives. ng-hide, ng-show, ng-init Then move on to reading how Angular implements some really simple directives. You will see a lot of $parse in there. Pay attention and start picking out patterns.
  32. Your journey: Learn how directives work. Google: “Dan Wahlin custom

    directives” Investigate how Angular implements simpler directives. ng-hide, ng-show, ng-init Take note of patterns and services being utilized and explore carefully Start with: scope, injector, interpolate Then start investigating some of these repeated patterns, like scope.eval, or providers. From there you will begin to truly grok how Angular works and do some amazing stuff.
  33. You can access the source for most components from the

    docs! By the way, you can get to each portion of the source by going to the docs for that component.
  34. PUP CLOSE Demystifying Angular by Going for a Walk I

    hope you learned something. Thanks a lot for listening. Here is your reward.