lundi 30 mai 2016

Laravel 5 macros with javascript

The situation - i wont make macros for Form, that include html and javascript. According to the http://ift.tt/1sWYVMt i made provider and include the macros file

    Form::macro('multiple_select', function($name, $list = array(), $selected = null, $options = array())
{
    $select =  Form::select($name, $list, $selected, $options);
    $button = '<button type="button" class="btn button_add_multi_item">
            <i class="fa fa-fw fa-plus"></i>
        </button>';
    $select = $select . $button;
    $select .= '<script type="text/javascript" src="' . @asset('assets/js/macros/multiple_select.js') . '"></script>';
    return $select;

});

This element include select and + element. By clicking on this element there should generate hidden input and span with selected option from the select. This is done by javascript in multiple_select.js file. The problem is - that jquery library file included in layout at the bottom of the page, and i got the error

Uncaught ReferenceError: $ is not defined

If i wrote in main layout -

        @yield('macros_scripts')
</body>

And in macros file

$select .= '@section("macros_scripts")<script type="text/javascript" src="' . @asset('assets/js/macros/multiple_select.js') . '"></script>@endsection';
    return $select;

then php generate string

@section("macros_scripts")@endsection

It doesn't handled by the blade template engine. So only 1 thing i can do - it's include jquery in the top of layout. But i beleave that there is other, correct way to include js in macros. Any tips, helps?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire