Who should format my data for display?

I have a django view, and this view returns a table that is populated asynchronously with an ajax call.

In terms of design, with which I should follow:

  • the django view called via ajax returns the contents of the table as a json response containing html markup for each cell. The javascript callback handler takes the contents and removes them in the table cells.
  • the django view called via ajax returns pure data about what should go to the table, again as a json response. The async javascript callback takes the data, formats it with the proper markup, and puts it in the table.

In other words, who should be responsible for marking up the formatting of the contents of the cell? view or javascript?

I will be tempted to say the first, as the view already returns the marked content. If it returns json containing tagged content, there is not much difference.

I would like to hear your point of view.

+3
source share
6 answers

If you fill the whole table, you can put your table in your own template and return the html table via ajax / json.

You will need to edit the source template to include the table template:

 {% include "myapp/_table.html" %}

And in the view, return the processed template as a json variable that your javascript will replace:

 return { 'table': render_to_string("myapp/_table.html", context) }

, , . , , , .

+3

( ).

, , .

, (, ), , JavaScript , JSON.

, , , .

+2

MVP, Django, View , , Presenter , . JavaScript , .

+1

Unabstrusive javascript, Hijax

, , , django.

django "if not ajax", ajax, div.

javascript.

, , , html.

+1

, , JSON.

, ( /). JSON, , .

HTML, JSON . , API .

, , ​​ John Resig micro-templating Closure Templates, HTML .

+1

, sine : ( ), JS (, )

0

Source: https://habr.com/ru/post/1730167/


All Articles