laravel добавить код javascript в раздел yield - PullRequest
0 голосов
/ 13 апреля 2019

Я делаю структуру компонента и добавляю пример компонента DataTable, и я хочу, чтобы при вызове {{Component :: DataTable ()}} класс данных имел метод сценария, и его нужно добавить в файл js yield в приложении. лезвие.

Директории

app
|--Library
|--|--Components
|--|--|--DataTable
|--|--|--|--DataTable.php
|--|--|--|--DataTableBuilder.php
|--|--|Component.php
|--|--|IComponentBuilder.php

index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>
<body>
    {{
        Component::DataTable()
    }}

   @yield('js')

</body>
</html>

DataTable.php

<?php

namespace App\Library\Components;

class DataTable {}

DataTableBuilder.php

<?php

namespace App\Library\Components;

class DataTableBuilder implements IComponentBuilder {

    public function __construct()
    {
        $this->component = new DataTable();
    }

    public function script()
    {
        return '
            console.log(\'test\');
        ';
    }

    public function toHtml()
    {
        return 'DataTable';
    }

}

component.php

<?php

namespace App\Library\Components;

class Component {

    public static function DataTable()
    {
        $builder = new DataTableBuilder();

        return $builder;
    }
}

IComponentBuilder.php

<?php

namespace App\Library\Components;

use Illuminate\Contracts\Support\Htmlable;

interface IComponentBuilder extends Htmlable {
}

в этом сценарии, как я могу добавить результат метода scripts () к выходу js. PS: я могу добавить более одного компонента в будущем.

...