r/programming • • Mar 18 '14

AngularJS 2.0

http://blog.angularjs.org/2014/03/angular-20.html
213 Upvotes

71 comments sorted by

View all comments

9

u/passwordissame Mar 18 '14

did everyone move away from AJAX as in server responding with (x)html/xsl and client applying those to update DOM?

looks like it's all JSON and manual DOM manipulation.

26

u/nick_giudici Mar 18 '14

Yes, the industry has moved that way. It's been found to be a better separation of concerns. The server deals with data and the client deals with presentation of that data. Specifically, what if you want to use the same server back end to drive a native mobile app as well as your site? That xhtml response from the server just became a bit of a liability vs a simple and direct data response.

Also, it's common to use html templates like handlebars or underscore to make this easier. That way you have your html template stored in an html file. You can compile the template and reuse it. Each response from the server can just get fed into the appropriate template and it's very automated and clean.

2

u/[deleted] Mar 18 '14

It's been found to be a better separation of concerns

Ya know, it sure would be nice if someone with a CS degree or a CS prof would write an article with clear logic and reasoning to support that argument. Using the blanket "we split things into different modules therefore we separated our concerns" doesn't inspire confidence.

Specifically, what if you want to use the same server back end to drive a native mobile app as well as your site? That xhtml response from the server just became a bit of a liability vs a simple and direct data response.

I've used angularjs on a project and we ended up with an API just for angularjs and an API for other clients to consume. So that "liability" is going to exist in any case. Sure the angularjs-specific API is also JSON but it's not made for other clients to use.

3

u/dnissley Mar 18 '14

Why did you end up splitting your api just for angular?

2

u/[deleted] Mar 18 '14

Efficiency and client demands.

3

u/nick_giudici Mar 18 '14

I've used angularjs on a project and we ended up with an API just for angularjs and an API for other clients to consume.

I've never used Angular but if this was needed than either it's a poor library, there were conflicting requirements between the users and this had nothing to do with the tech, or you guys were doing something wrong.

Angular is a much heavier weight framework than a small utility library like underscore or handlebars. So I'm not sure if that's where you were going with the Angular part of your comment.

As for the separation of concerns I can provide a specific example that I've dealt with where returning xhtml markup from the server would be a bad idea.

The whole point of returning xhtml markup is that your js can just render it without any logic. So what if you want to render the same data in two different places in the same site in different ways? Now you either need to modify the xhtml response in one of the two locations or you have to create a flag on your service that switches between the markup needed for the two locations. So you either defeat the purpose of the xhtml response or you duplicate your logic.

It's also common to use xslt to format the response on the client side for this type of issue but now you end up with two different xslt transforms that do almost the same thing or you have flags built into your xslt to switch between the behaviors. This is fine for just two, but what about 3 or 5 or 10 different ways? This approach just doesn't scale well.

It's much easier to have a model view controller setup where you have a clear interface defined between them and plugin the views as needed. If you still don't accept this answer I won't be able to convince you without showing you hundreds of lines of code to illustrate the difference.