javascript - Store info i element, a good practice? -


Say I have a list of movies where I can replace the title, rating and cost of films with JavaScript and Jquery via HTML Control. . The original values ​​of these variables will be given when the page will load. So far, I load the data in elements like this:

  and lt; Div class = "movie0" data-rating = "320" data-name = "movie-name number1" data-cost = "26>   ; / Div> & lt; div class = "movie1" data-rating = "110" data-name = "movie-name number 2" data-cost = "18" & lt; img src = "Movie2.png" /> & lt; / div & gt; & lt; div class = "movie 2" data-rating = "315" data-name = "movie-name number 3" data-cost = " 25 "& gt; & lt; img src =" movie3.png "/> & lt; / div>  

HTML control, jQuery .data () function Change the data using.

For the question now. It would be better to load the information into a JSON object and then the ID (or a data attribute) is related to an index in the JSON object. Instead of changing the elements, they will replace the index in the JSON object related to the element. What's better? If I only have the rating variable for priority?

EDIT: I am asking this question because I have read and heard that the DOM stores And changing information is disabled.

There is no "right" answer

Using meta data on HTML objects Advantages of doing:

  1. Data can be retained by a non-developer by modifying the attributes on HTML.
  2. By replacing the HTML, the item can be added, removed, or edited by a non-developer.
  3. A different movie object describes itself all in one place. You do not have to maintain both the HTML and the relevant code.

The advantages of using pre-built javascript objects are:

  1. Potentially high performance (although it is not clear that performance differences will not be significant here ).
  2. Modifying the data with the javascript code can be simple in a javascript object.

I do not know what you heard about "DOM collecting and changing information", it is disabled "" It really depends entirely on what you do And what kind of data you are changing and how much performance matters in any way. It is slow to modify the DOM in such a way that causes relay and red color. However, DOM does not relay or repaint from reading a piece of meta data from the HTML object.

If you have a tight loop that was very sensitive to the display, then you should use the data in JavaScript Object, not the attribute on the dom objects, but displaying a dome attribute is usually an important one. There is no issue, so issues like readability and maintenance are often more important and decide.


You are not using the term json word fairly, I think what you are asking about is a "javascript object" or "javascript literal", which means that you have your javascript Declares the javascript variable in the code steadily. JSON is a text format for specifying or interacting JavaScript data in a pure string format. JSON should be parsed to convert to real JavaScript variables.


Comments