0

I am building a mobile app with Jquery mobile. Some data I need to request from an API and display inside the app.

now I have two options:

  1. My API returns Json and I need some Javascript to format it to display in my app (it'a a list , so I would loop through it and append to the list)

  2. My API returns ready HTML which I just need to include in some container. For this case I found this method: $( "#result" ).load("ajax/test.html" );

Which one is best practice and how would you realize itt

almo
  • 6,107
  • 6
  • 43
  • 86
  • 2
    There are some pretty in depth answers here : [Why is it bad practice to return generated html instead of json](http://stackoverflow.com/questions/1284381/why-is-it-a-bad-practice-to-return-generated-html-instead-of-json-or-is-it) – Kevin F Jun 30 '15 at 22:48
  • Why not use [underscore](http://underscorejs.org) templates? – kidA Jun 30 '15 at 23:16
  • Use first option. Scalable, maintanable – Toumash Jul 01 '15 at 07:21

1 Answers1

1

I have certainly seen it done both ways. Here are some "bright-line rule" suggestions that you might consider.

(1) If the content is going to be treated by the app as "a black box" ... "don't look at, we're not going to have to interact with it, all we need to do here is to display it, whatever it is" ... then it might be entirely appropriate to let the server do all the work for you. (Especially if there are many possibilities, and "the server would know best," and "the app really doesn't have to give-a-damm.") You want to avoid creating unnecessary dependencies or duplication between the app and the server.

But ...

(2) If the content is something that the app is going to have to interact with, and/or is going to need to "style" or somesuch, then the logic within the app should probably be doing it. You want to avoid creating unnecessary dependencies or duplication between the server and the app. (Notice how the words, "server" and "app," are reversed in this paragraph.)

The concept is sometimes called "separation of concerns." If whatever-it-is is "more the server's concern than the app's," and "the app can afford to be 'unconcerned,'" then the server can hand-over au fait accompli. Otherwise, it probably should be handing you data.

In any case: "it is an engineering judgment-call for you, the engineer, to make and defend."

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41