kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

Load assets from CDN #223

Closed robov closed 7 years ago

robov commented 7 years ago

Prerequisites

Steps to reproduce the issue

1. 2. 3.

Expected behavior and actual behavior

When I follow those steps, I see...

I was expecting...

Environment

Browsers

Operating System

Libraries

Isolating the problem

robov commented 7 years ago

How can I load the required js assets from a CDN ? To speed up my site loading select2.js and select2-krajee.js from a CDN would increase in must faster response times

MartinKolarik commented 7 years ago

You can use https://www.jsdelivr.com/package/npm/select2 and https://www.jsdelivr.com/package/gh/kartik-v/yii2-widget-select2

robov commented 7 years ago

Thanks... but where do I specify that ?

Like my bootstrap in main.php

            'bundles' => [
//                // use bootstrap css from CDN
                'yii\bootstrap\BootstrapAsset' => [
                    'sourcePath' => null,   // do not use file from our server
                    'css' => [

or do I need to create a new asset file and load that somehow ?

kartik-v commented 7 years ago

This is a piece of cake to do in Yii 2. Just override your asset bundle in your yii2 application config as mentioned in this customizing asset bundle guide for Yii 2.

robov commented 7 years ago

Thanks... apparently I am still doing something not quite right, as I get a select2 is not a function

The original working code loads the local assets

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="/assets/46aff460/js/select2.full.js"></script>
<script src="/assets/46aff460/js/select2-krajee.js"></script>

When I try to override with

            'kartik\select2\Select2Asset' => [ 
                'sourcePath' => null,   // do not publish the bundle
                'js' => [ 
                    'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.full.min.js',
                    'https://cdn.jsdelivr.net/gh/kartik-v/yii2-widget-select2@2.1.0/assets/js/select2-krajee.min.js',

                    ] 
            ],

The the code loads

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.full.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/kartik-v/yii2-widget-select2@2.1.0/assets/js/select2-krajee.min.js"></script>

But then I get a error "VM1202:2 Uncaught TypeError: jQuery(...).select2 is not a function" when I try to use select2 in my modal (with the original code it works like a charm... just slow loading....

Any suggestions ?