A
A
Artem00712017-09-15 20:56:42
JavaScript
Artem0071, 2017-09-15 20:56:42

How to find blocks?

I get a list of blocks like this:

let DOMBlocks = document.getElementsByClassName('obj-block');  // получаю блоки
let StoreBlock = this.blocks; // массив блоков

// Вот так выглядят блоки в сторе:
blocks: [{
    id: 1,
    position: {
      top: 1,
      left: 10
    }
  },
  {
    id: 2,
    position: {
      top: 200,
      left: 350
    }
  }
]

// блоки рендерятся в следующее:
<div :class="'block obj-block block-' + block.id">
  </div>

That is, it turns out that each block has an id in the class.
(Is this generally normal, or is it better to do it somehow through id="block-IdOfBlock"?)
In general, I stumbled on a regular expression, I can’t figure out how to isolate these blocks:
let DOMBlocks = document.getElementsByClassName('obj-block');

          let StoreBlock = this.blocks;

          let blocks = [];

          for (let i = 0; i < DOMBlocks.length; i++){
            console.log(DOMBlocks[i].className);
          }

// и как их правильно добавлять в массив блоков, чтобы получилось это:
blocks = [
  { 
    id: 1,
    position: {},
    ...,
    elementInDOM: DOMBlocks[i]
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Dubinin, 2017-10-09
@romash

blocks.push({
    id: /block-(\d+)/.exec(DOMBlocks[i].className)[1] * 1,
    ...
});

In general, to store any data in the layout, it is better to use data-attributes
<div :class="'block obj-block'" :data-id="block.id">
  </div>

I have no idea what kind of template engine you have, but I think you will get the point

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question