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.

7

u/kdeforche Mar 18 '14

The industry? Or the startups?

14

u/nick_giudici Mar 18 '14

I work for a mutual fund company and we're using json based services. We have a flag that customers can set to get an xml version of the response but the majority of our usage is json. I have friends in the medical tech industry and they too are making the transition to json as their interchange format of choice.

So in my experience both industry and start ups are moving away from xml. But as always, start ups are able to make the change more quickly. You'll be seeing xml in industry for the next 50-100 years. Hell, you still see fixed with ebcdic encoded files on a regular basis in industry and government (an old ibm mainframe standard).

To be clean, I'm not saying that you can't ind a lot of xml in industry. I'm just saying that momentum has shifted to json and xml is looked at as deprecated.

3

u/depressiown Mar 19 '14

Industry, too. We have a mature, 15-20 year-old B2B software package that has gone through several transformations with regards to the UI. Right now, we're migrating a lot of Flex into modern HTML5/js screens and communicating via ajax/json. Obviously, this is an extremely long-term effort and we're doing it in pieces, but it's definitely the direction.

Additionally, new products we make keep popping up and they're all using the same model: REST services that return JSON and the UI communicates with those. Completely separation of roles makes for easier separation of development efforts -- you just work on the interface between the back and front ends.

Again, this is a 30 year-old B2B software company that's publicly traded, not some start-up.

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.

-4

u/passwordissame Mar 18 '14

HTML is data. It's representation of a resource. A pretty good one, I must say. I'd argue that it's easier to write clients when resources are represented in HTML or other hypermedia. In case of HTML, there are many good HTML parsers that let you use xpath and/or CSS selectors. Combined with RDFa or microformats, you can easily build robust clients. HTML even defines <form> and other links for updating resources.

With JSON, in my experience, it's very easy to write brittle clients that will break if server changes representation just a little. There's JXON, but I haven't used it in production.

15

u/nick_giudici Mar 18 '14

It's your experience vs the industry consensus. The industry is currently shifting away from HTML services and to JSON.

My belief as to why is that HTML is a markup language specifically meant for display in a web browser and JSON is an object serialization format. Even with pure XML you have issue when you have a block like:

<issues>
  <issue>Data</issue>
</issues>

Should issues be represented internally as an object/map or as an array? Will I ever get more than 1 issue tag? In JSON you have maps and arrays that map directly to the native constructs in your language in a 1 to 1 fashion. XML has a many to many relationship. I have at least 3 ways to represent key/value pairs in XML but only 1 in JSON. I have structures in XML where I'm not sure if they are an array or a key value pair map.

The common way to solve this in xml is by using a schema but that is the definition of brittle. Now you have to bump the api version for every little change even if you are just adding a field. In a JSON based service this is not the case.

Then we add in the fact that for correct display in your page you will likely need classes and divs that are not data. Those are clearly display markup and make your data tightly coupled with the display. Your xpath ends up not representing your business data instead ends up representing your display structure.

-12

u/passwordissame Mar 18 '14

I hope you heard of semantic markup. And give me one crawler/search engine of JSON resources. I give you few search engines of HTML resources: google.com bing.com duckduckgo.com

9

u/nick_giudici Mar 18 '14

Wow, you are being very immature about this. Are you actually insulted that someone doesn't like html/xml as much as you?

I know about semantic markup but you failed to address the other issues that I talked about with xml and html in general. Also, the industry has spoken and is moving away from having web services returning directly displayable markup. Here's what I was able to lookup for you.

My search: https://www.google.com/search?q=json+vs+xml+web+services

Top 3 hits:

http://digitalbazaar.com/2010/11/22/json-vs-xml/ (tl;dr: Use Json)

http://stackoverflow.com/questions/2636245/choosing-between-json-and-xml (Top rated answer "JSON for web services answering in-browser callbacks, and XML for, well, XML for when, well, hmmm. :-)")

http://www.seguetech.com/blog/2013/04/01/xml-vs-json-web-services-best-choice (tl;dr: Use Json)

Json is smaller, parses much faster with less client cpu usage (great for mobile), you don't have to use xpath which is slow as hell and completely outside the natural paradigms of most languages, and has less edge cases where it fails like empty or single element arrays.

1

u/Azzk1kr Mar 18 '14

I've been in a fight with myself regarding JSON vs. XML. Coming from an XML world, I can say that XML is far from ideal. The only thing that keeps me sort of fixated to XML based services, is its self describing interface through XSD. I find that missing in JSON.

Your previous argument that you'd have to bump the API version for every change is correct though, but is this really a bad thing? In JSON this would also be the case, albeit through a change in the API URI or whatever.

2

u/nick_giudici Mar 18 '14

In my team we change version numbers whenever we have a non backwards compatible change. Our clients are told that they have to be able to deal with new fields being added without a version update.

Getting all our clients to update to the latest version takes months even if it's better in every way. We try to make as few api versions as possible. It's much better for us and our clients to only change version numbers when we need to.

1

u/Azzk1kr Mar 19 '14

XSD allows you to validate the input before processing it. How do you solve that with JSON? Manually checking each field, or something clever? I'm asking, because I often have services with fairly large requests in XML, and I want to make sure both parties use that same contract to catch discrepancies as soon as possible.

1

u/nick_giudici Mar 19 '14 edited Mar 19 '14

Have you ever run into the case where your XSD validated but the client was still getting errors? Maybe you had some criteria that couldn't be validated in XSD like a field that has to match a row in your database. How do you check for those types of issues?

Personally, I've usually found that XSD only gave me a false sense of confidence. What I've found is that intelligent validation and error handling no matter what format you are using.

People seem to forget that XSD and XSLT are just as complex as "real" code. There seems to be the attitude of "I'm able to validate and transform my xml without code!" You still have to version control your XSLT and XSD, you still have to test and debug them, update and maintain them. Hell, XSLT is turing complete. So no, you aren't doing transforms and validation without code, you just did it without Java or C#.

Edit: If you really want a validating schema for your json file you could check out http://json-schema.org/

→ More replies (3)

3

u/Rainfly_X Mar 18 '14

Semantic markup isn't really useful for APIs, though. Web pages, sure, but if you're worried about your AJAX API being friendly to screen readers, rather than Javascript/other languages, you have some amusingly misplaced priorities.

7

u/bkv Mar 18 '14

HTML is data. It's representation of a resource.

HTML is markup. It defines the structure of a document. It contains data, but is not the data itself.

Ideally, a client and server exchange data. It is the client's responsibility to determine how that data is displayed. Different clients will want to display the data differently -- A web client may want to display it in a table and a mobile client will want to display it in some sort of native widget.

I think you've become too comfortable in the world of elaborate hacks we've lived in for so many years, where the line between client and server is blurred. Sure, it's familiar, but it's far from ideal.

3

u/bringerofnative Mar 18 '14

Yes and the only acceptable punishment for this transgression is death.

-9

u/deadstone Mar 18 '14

You're confused; That IS AJAX. AJAX basically means using javascript to change the page instead of going off the page. What you seem to have meant is the traditional way of having each page at a separate url.

11

u/bkv Mar 18 '14

You're confused;

Actually, you're confused. AJAX stands for "Asynchronous Javascript and XML" although it's become a blanket term for asynchronous javascript and XML/JSON.

AJAX basically means using javascript to change the page instead of going off the page.

Nope, that's just DOM manipulation, and people have been doing it long before asynchronous requests were introduced in the browser.

-1

u/deadstone Mar 18 '14

The original question still makes no sense as wrong as I am; They're asking why people are using DOM manipulation instead of DOM manipulation.

11

u/indeh Mar 18 '14

The original question is asking if there are people who still have the server return blocks of HTML which they just insert into the page, or if everybody expects the server to return JSON formatted data and build all the DOM elements on the client side.

0

u/cybercobra Mar 18 '14

When was XSL even mildly popular with the mainstream?