Advertisement
ArtSemkin

Untitled

Jun 30th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $.fn.getAllAttributes = function () {
  2.     var
  3.         elem = this,
  4.         attr = {};
  5.  
  6.     if (elem && elem.length) $.each(elem.get(0).attributes, function (v, n) {
  7.         n = n.nodeName || n.name;
  8.         v = elem.attr(n); // relay on $.fn.attr, it makes some filtering and checks
  9.         if (v != undefined && v !== false) attr[n] = v
  10.     })
  11.  
  12.     return attr;
  13. }
  14.  
  15. function syncAttributes($sourceElement, $targetElement) {
  16.  
  17.     if (!$sourceElement.length || !$targetElement.length) {
  18.         return;
  19.     }
  20.  
  21.     var sourceAttrs = $sourceElement.getAllAttributes();
  22.  
  23.     $targetElement.attr(sourceAttrs);
  24.  
  25. }
  26.    
  27. function PJAXUpdateNodes(data) {
  28.  
  29.     return new Promise(function (resolve, reject) {
  30.  
  31.         var
  32.             $nextContainer = $($.parseHTML(data.next.html)),
  33.             nodesToUpdate = [
  34.                 '#js-burger',
  35.             ]; // selectors of elements that needed to update
  36.  
  37.         $.each(nodesToUpdate, function () {
  38.  
  39.             var
  40.                 $item = $(this),
  41.                 $nextItem = $nextContainer.find(this);
  42.  
  43.             // sync attributes if element exist in the new container
  44.             if ($nextItem.length) {
  45.                 syncAttributes($nextItem, $item);
  46.             }
  47.  
  48.         });
  49.  
  50.         resolve(true);
  51.  
  52.     });
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement