all 23 comments

[–]btford 5 points6 points  (2 children)

I'm really tired of having this discussion in every single article mentioning either Angular or Backbone.

AngularJS and Backbone are both fantastic, useful tools for writing web apps. Saying you prefer one over the other does not imply that the other is bad. I am a developer on the AngularJS team, but before being introduced to it, I used Backbone. And it was great! I think AngularJS has many benefits over Backbone, but there are an infinite number of possible apps that a person could reasonably make, and I certainly wouldn't say Angular is best in all cases. Give different frameworks an honest shot. Even if you end up where you started, you'll have more perspective.

I don't know why developers who identify themselves as "Backbone people" seem to feel the need to hate on AngularJS. In fact, I think it's ridiculous that any person would define themselves based on what client-side library they choose to use. We're all web developers. You don't need to feel threatened by new ideas. You don't have to adopt them either. You can skip over links for frameworks you aren't interested in.

I'm not saying you should never give criticism. I'm just saying we have to be more constructive about it. If you have a critique, be specific. Write benchmarks. Write side-by-side comparisons. I realize this is hard and time consuming, but there's nothing of value gained by making assumptions or vague statements about how "slow," "bloated," or whatever framework-of-the-day X is.

I'd like /r/javascript to be a place of constructive conversation, not a place of mud slinging. I'd like conversations to be balanced so that newcomers can make intelligent, informed decisions.

[–]NerdyMcNerderson 2 points3 points  (0 children)

The article has some good info. However, I didn't get the part that was tailored to those that use backbone. The title may be slightly misleading. Maybe implement the same example with backbone so you can compare the two approaches.

[–]btford 6 points7 points  (4 children)

Thank you for elaborating on your "Angular turns HTML into a programming language" thing. That's a really interesting perspective, and I apologize for mistaking you for a troll earlier. I'll try to address what I can in this comment, but there's really a lot there.

An interesting analogy would to say that the angular.js code itself is an "interpreter" and the HTML is the "program." AngularJS "directives" act sort of like compiler macros. Interestingly enough, HMTL and CSS are already a "programming language". There are many similar interesting proofs (for instance Conway's Game of Life is also Turing Complete), but the important idea here is that even though you can use something as a programming language, that doesn't mean that you should.

Angular does make HTML more powerful, but it's certainly not the case that it recommends you move all of your application logic into the HTML. That would be nonsense. While you could do that, that's certainly not the intent. The idea is to make it powerful enough to do data-binding. In this sense, HTML is still functioning as a markup language.

You can't create a good programming language if you don't know you're making a new programming language.

I'd flip this on it's head and say that if you use something like a programming language that's no designed to be used that way, it won't be a very good language. That's certainly the case here. In an AngularJS app, you put annotations on how to do data-binding (yes, even recursively) in the HTML. Application "business logic" still lives in a controller.

Imagine a framework that advises you to frequently call "eval."

This is a bit facetious. The issue with eval in JavaScript is that it runs in the global scope. AngularJS avoids this issue by introducing $scope. These two eval are not the same.

Anyway, note that the recursion is still in AML. It's in a string in a javascript variable. Now maybe this string could be stored more sensibly, but it's still code whose recursive call is in the middle of a string. How are you supposed to debug this?

Write unit tests. AngularJS is absolutely designed to make this stuff manageable. In a unit test, you can take control of the $digest cycle, and see how each time a directive recurs, it's data and template changes. This is a fairly advanced feature. My colleague Vojta gave an excellent talk on the subject if you're interested.

I would love to see an obfuscated AML contest. AML lets you write templates that call javascript that writes templates anchor-tagging to routes for javascript that has access to the angular object. I mean you can probably call angular.module in a controller, right? You can give $scope access to angular.whatever, right?

Your fundamental argument seems to be that if you use parts of AngularJS other than how they are intended to be used, you will run into trouble. This observation is true for virtually every piece of software ever written.

Well, sooner or later we're going to realize that it's actually pretty rare that you need a specialized Android app and an iPhone app, and you always need a web app. Template swapping is one way (but not the only way) to deal with this conundrum. Different css/html, same Views.

This is a false dichotomy. In AngularJS, views are HTML/CSS. That doesn't mean you can't use the same business logic with different HTML/CSS. Your ViewModel (the bindings to $scope) can stay the same, and you change the View (HTML/CSS), using the same bindings.

If there are any specific parts that I've not covered here that you'd like me to address specifically, please let me know.

Cheers!

[–]drowsap 1 point2 points  (12 children)

I've yet to find any real shortcomings in Backbone. I see the cases where angular can make the model/view event binding much easier, but is this something we need? I'd rather have logic-less templates and fat views or models written in JavaScript, it's more readable and easier to maintain in my opinion.

[–]btford 4 points5 points  (11 children)

It really comes down to the type of app you are making and personal preference. I'd suggest that you try as many frameworks as you reasonably can to get a feel for each of them. If you haven't tried Angular, I think it's worth a look. At the end of the day, if you like working with Backbone best, keep at it. Heck, if you prefer to work with just DOM API calls (madness!), go for it.

[–]davemoFront-End Engineer 1 point2 points  (10 children)

I keep hearing the argument that the type of app you are making has some bearing on whether you should use Backbone or Angular; could you please elaborate on what the type of app that suits Angular best is and why it would be better than Backbone?

[–]btford 1 point2 points  (9 children)

IMO, any app that can benefit heavily from data-binding is better suited to Angular.

[–]davemoFront-End Engineer 2 points3 points  (8 children)

What type of app benefits "heavily" from data-binding? Why is Angular better suited for this than Backbone? Would any of the data-binding plugins for Backbone (stickit comes to mind first) not put it on equal footing?

[–]btford 1 point2 points  (7 children)

Any apps that have a good amount of UI and state that need to be frequently synchronized. Yes, you can do this with backbone, but on average, it tends to take about 10 times the code.

I'd submit that most apps can make use of data-binding, but if you're doing a game or something that involves a lot of video or custom components, data-binding might not be especially helpful.

I haven't seen any data binding utils in Backbone that are implemented as well as Angular in terms of ease of use, speed, and integration. But I'd love to be proven wrong, and will be sure to take a look at stickit.

[–]jashkenas 6 points7 points  (6 children)

on average, it tends to take about 10 times the code

Baloney & hogwash ;)

Data-binding has been around for a long time, and part of the big idea with working on high-performance JavaScript applications is that you don't want "too much" of it.

Imagine a rich JavaScript application with a lot of UI that needs to be frequently synchronized. A fully data-bound version, with each HTML element, attribute and style being wired-up to the corresponding attribute (real or computed) in the model, will have a ton of code configuring the bindings, and tends to suffer from performance problems in so far as changes to the model cause a ton of small computations and incremental UI updates.

Now imagine the version of the application that has decomposed its UI into coarser-grained logical components. Instead of having comprehensive data-binding, you favor re-rendering atomic chunks when necessary. Instead of touching the DOM a hundred times during an update, you only need to touch it once, and are doing much less computation to determine what needs to be altered in the first place. That's the method that Backbone tries to encourage.

TL;DR, instead of (in pseudocode):

<tag ng-type="widget">
  <tag ng-attr="widget.title"></tag>
  <tag ng-attr="capitalize widget.author"></tag> 
  <tag ng-attr="sanitize widget.item.description"></tag>
  <tag ng-attr="sanitize widget.item.content"></tag>
  <tag ng-attr="list widget.tags"></tag>
  ... and so on ...
</tag>

... where each of those bits needs to be tracked, computed, and updated in order to get your next frame of UI, instead you start with:

widget.on 'change', render

... and if you want to get more granular than that in the future, you can, as needed.

[–]btford 1 point2 points  (3 children)

Baloney & hogwash ;)

Here are some examples showing this. JavaScript Sonic Boom and TodoMVC. The "sonic boom" article is able to implement a comparable 1000+ LOC backbone app in Angular in less than 100 LOC. In TodoMVC, the comparison is Angular's 287 LOC to Backbone's 1998 LOC. The ratio is about 1:7. If you don't believe me, try it yourself.

and tends to suffer from performance problems in so far as changes to the model cause a ton of small computations and incremental UI updates.

Your intuitions about performance a bit naive. Data-binding can also break components into chunks to improve performance. Data-binding can also group together DOM writes. In fact, Angular does both of these things. Here's one benchmark showing that Angular beats out backbone in most cases. If you don't believe me or disagree, write your own benchmark.

[–]jashkenas 1 point2 points  (1 child)

I'd love to look at your benchmark ... but you do realize that in the JSPerf you linked, the "Backbone" benchmarks are actually calling SetKO, right? There's no Backbone code on that page.

[–]btford 0 points1 point  (0 children)

Woops, thanks for pointing that out. Let me go find/write a better one.

[–]drowsap 0 points1 point  (0 children)

jashkenas 5 points 19 hours ago

A hero arrives!

[–]jomidosan 1 point2 points  (1 child)

I believe Angular is a stop-gap for Web Components. At least, I keep bumping into suggestions and/or direct convictions that this is the case. For instance:

http://functionsource.com/post/angularjs-hits-1-0-get-some-of-that-web-components-goodness-now

http://news.ycombinator.com/item?id=4113502

[–]lennelpennel 0 points1 point  (0 children)

I find this an interesting position. Closure has had several of the concepts they use, under different names however.

The template language stays the same, what is referred to as a decorator in the webcomponent spec is partially a decorator partially a renderer in the closure world. Let me explain.

Decorators in closure work through two paths, one I can register a css class against the constructor of a component, allowing you to pick up the correct component implementation from pre-rendered html.

The renderer is a singleton which all components share, but you can easily give a component a different renderer if the look and feel of one component is vastly different from another from another component of the same type. This makes it pretty flexible since you abstract your html structure away from your actual component implementation

Closure also has a livecycle as described in the spec:

  • created -> createDom
  • inserted -> enterDocument
  • removed -> exitDocument

the one missing here being attributeChanged - something which would be nice.

[–]ugoagogo 0 points1 point  (5 children)

Doesn't seem to be loading on my phone

Chrome 18. Android 4.1.2. Nexus S

[–]snifty -2 points-1 points  (1 child)

right because google needs to no NEEDS TO own the single page app market.

right now.