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.

-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/