Дом Кеш

Dom Cache используется для сохранения визуализированных элементов для ускорения POS.

Чтобы добавить что-то в Dom Cache, вам нужно сделать что-то вроде этого:

this.cache = new screens.DomCache();
this.cache.cache_node(key, value);

Чтобы восстановить визуализированный элемент из кэша, сделайте что-то вроде этого

this.cache = new screens.DomCache();
var cache = this.cache.get_node(key);

Вот полный пример из модуля Point of Sale <https://github.com/odoo/odoo/blob/12.0/addons/point_of_sale/static/src/js/screens.js#L761-L789>`__:

Целью этого кода является оптимизация рендеринга элементов в POS. Каждая новая загрузка POS использует данные из `` DomCache`` - тем самым экономит время на рендеринг новых элементов.

Давайте возьмем пример:

Модуль `` POS Order History``

В этом в этом модуле <https://github.com/it-projects-llc/pos-addons/blob/12.0/pos_orders_history/static/src/js/screens.js#L159-L198>`__` DomCache` используется при отображении списка заказов.

После первой загрузки POS-элементы заказов, которые были обработаны (* HTML code *), сохраняются в Cache.

После перезагрузки POS проверяется наличие сохраненных элементов в Cache, и эти данные используются при обработке заказов.

init: function(parent, options) {
  this._super(parent, options);
  //object of DomCache,which we will use in order to address the methods of this object
  this.orders_history_cache = new screens.DomCache();
},
render_list: function(orders) {
  var contents = this.$el[0].querySelector('.order-list-contents');
  contents.innerHTML = "";
  for (var i = 0, len = Math.min(orders.length,1000); i < len; i++) {
    var order = orders[i];
    // getting cache via key
    var orderline = this.orders_history_cache.get_node(order.id);
    var lines_table = this.orders_history_cache.get_node(order.id + '_table');
    /* here we check for the presence of cache among existing data
    if there is no cache, then we render elements and save into cache
    if the cache exists, we just use it
    */
    if ((!orderline) || (!lines_table)) {
    // rendering of elements may take time
      var orderline_html = QWeb.render('OrderHistory',{widget: this, order:order});
      orderline = document.createElement('tbody');
      lines_table = document.createElement('tr');
      var $td = document.createElement('td');
        if (order.lines) {
         $td.setAttribute("colspan", 8);
        }
      lines_table.classList.add('line-element-hidden');
      lines_table.classList.add('line-element-container');

      var line_data = this.get_order_line_data(order);
      var $table = this.render_lines_table(line_data);

      $td.appendChild($table);
      lines_table.appendChild($td);

      orderline.innerHTML = orderline_html;
      orderline = orderline.childNodes[1];
      //save the result into cache
      this.orders_history_cache.cache_node(order.id, orderline);
      this.orders_history_cache.cache_node(order.id + '_table', lines_table);
    }
    contents.appendChild(orderline);
    contents.appendChild(lines_table);
  }
},