N
N
Nikolino2018-10-11 06:11:28
Laravel
Nikolino, 2018-10-11 06:11:28

How to move dynamic js out of a view into a separate file?

It is not recommended to use inline js in templates. But what about the js in which the data of the model objects is substituted, let's say, going through the collection with foreach to display the data on chart.js charts.
For example, like this:

datasets: [
                        @foreach ($demandArray as $keyword => $values)
                        {
                            label: '{{$keyword}}',
                            data: [@foreach($values as $key => $value) {{$value}} @endforeach],
                            fill: false,
                            borderColor: colors.pop()
                        },
                        @endforeach

The view is large, it has a lot of all sorts of graphs (pie, column, etc.), as a result, you get a lot of inline script inserts, which clutters up the code.
Is it possible to put all the js logic in a separate file, so that foreach will go through it all there, and then compile it into one js file?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question