S
S
Sergey Romanov2014-12-28 16:18:28
Angular
Sergey Romanov, 2014-12-28 16:18:28

Auto-defined directive?

It's complicated. So there is something like an editor. Block. That is, you can insert blocks of different types into it and swap them. For example, a text block, a heading block, a list block, a separator block.
The editor is a directive, and blocks are also directives. For example, we have a text variable.

$scope.text = [{
  "type":    "title",
  "state":   "preview",
  "content": {
    "level": 1,
    "text":  "Это начало статьи"
  }
}, {
  "type":    "markdown",
  "content": {"text": "Теперь работаем надо самим уроком."},
  "state":   "preview"
}]

In this example, there are 2 blocks. Title and text that can be written in markdown.
Well, the directive, it's something like.
<editor>
    <block>
        <title content="content" />
    </block>
    <block>
        <markdown content="content" />
    </block>
</editor>

It's super simplistic, of course. In real life, this is how ng-repeat stands and each block is compiled via $compile, since I dynamically insert them based on $scope.text[i].type.
There are a lot of different blocks, from text to video, graphics, ...
It's convenient that a separate type of block is a separate directive. It is very easy to expand and add new blocks.
Now a question. Maybe it's obvious, I'm sorry, but I can't figure it out.
How can I automatically get a list of all possible included directives of this type. In other words, at the bottom of the editor, there is a choice of which block you want to add. How can I make it so that if someone just added a file with a block directive to script src that the block would automatically appear there?
I believe that all editor block type directives should be moved to a separate module. And what if everyone added directives, then only added to this module.
Another question, how to attach metadata to a directive? For example block icon, block name, some behavior parameters?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TekVanDo, 2014-12-28
@TekVanDo

Well, what comes to me:
1) Make a service responsible for connecting directives.
2) When a directive module is connected, it registers itself in the service.
3) The parent directive through the service understands which blocks it can manipulate.
about weather data, why not pass them as attributes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question