F
F
fierfoxik2016-12-12 18:57:48
JavaScript
fierfoxik, 2016-12-12 18:57:48

How is it easier and more correct to store and retrieve json data?

At one time I used two options with storing json data, in a variable

var storage = JSON.parse(`{
        "id": "4",
        "name": "Товар 4",
        "price": "2500"
}'

And in json file.
In the variant with a variable, I rendered js, searched for the date id and stuffed the content already from the lines.
In the version with the json file, I used underscore.js to render json and, thanks to the library, inserted it into js in a loop
<% _.each(goods, function(good) { %>
             <div class="basket-item js-cart-item" data-id="<%= good.id %>">
              <div class="basket_item__image-link"></div>
              <div class="basket-item__main-title"><%- good.name %></div>
              <div class="page__basket-line"></div>
              <div class="price_theme_basket-item"><span class="price__item"><%= good.price %></span> руб.</div>

               <div class="cart-clicker">
                <div class="input-quantity__button input-quantity__inc js-change-count"
                        title="Увеличить на 1" 
                        data-id="<%= good.id %>" 
                        data-delta="1">
                  </div>

                <div class="input-quantity__val">
                    <input class="input-quantity__val-input js-count" type="text" value="<%= good.count %>">
                </div>

              <div class="input-quantity__button input-quantity__dec js-change-count" 
                        title="Уменьшить на 1" 
                        data-id="<%= good.id %>" 
                        data-delta="-1">
                </div>
            </div>
              
            <div class="page__basket_min-line"></div>
            <div class="basket-item__main-delete js-remove-from-cart" data-id="<%= good.id %>">Убрать</div>

            
      </div>
      <% }); %>

'use strict';

// Модуль каталога
var catalog = (function($) {

    // Инициализация модуля
    function init() {
        _render();
    }
  function init_six() {
      _renders();
    }

    // Рендерим каталог
    function _render() {
        var template = _.template($('#catalog-template').html()),
            $goods = $('#js-catalogue-list');

        $.getJSON('data/goods.json', function(data) {
            $goods.html(template({goods: data}));
        });
    }
    



    // Экспортируем наружу
    return {
      init: init,
      init_six: init_six
        
    }
    
    function _renders() {
      var template = _.template($('#catalog-template').html()),
          $goods = $('#js-load-catalogue');

      $.getJSON('data/six.json', function(data) {
        $goods.html(template({goods: data}));
      });
    }
  
    
})(jQuery);

Based on this, the question arises, which method would be better and easier to store data and get it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
akzhan, 2016-12-12
@akzhan

too general question. even the template engine is incomprehensible.
and in general your example is written as

var storage = {
        "id": "4",
        "name": "Товар 4",
        "price": "2500"
};

J
Jony1337, 2016-12-12
@Jony1337

Well, you can encode from json to md5 or base64 and store it in the database, why not an option?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question