I
I
Ilya2015-06-04 12:35:28
Yii
Ilya, 2015-06-04 12:35:28

How can I disable ClientScript script packages in Yii?

In main.php config:

'clientScript'=>array(
            'coreScriptPosition'=>CClientScript::POS_END,
            'packages'=>array(
                'jquery'=>array(
                    'baseUrl'=>'/themes/classic/js/',
                    'js'=>array('jquery.min.js'),        
                ),
                'bootstrap'=>array(
                    'baseUrl' => '/themes/classic/plugins/bootstrap/',
                    'js'=>array('js/bootstrap.min.js'),
                    'css' => array('css/reset.css','css/bootstrap.css'),
                    'depends'=>array('jquery'),    
                ), 
                'theme'=>array(
                    'baseUrl' => '/themes/classic/',
                    'js'=>array('js/site.js'),
                    'css' => array('css/style.css'),
                    'depends'=>array('jquery','bootstrap'),
                ),

In the most used controller:
public function beforeAction($action) {
        if(parent::beforeAction($action) ) {
            Yii::app()->clientScript->registerPackage('theme');
            return true;     
        }
        return false;       
    }

public function actionQuickview() {
        $this->layout = 'blank';
        $id = Yii::app()->request->getParam('id',0);
        //$this->disableScript(array('quickview')); // В идеале как-то тут отключить подключение THEME package
        $product = Goods::model()->findByPk($id);
        
        if($product) {   
            $this->render('_goods_quickview',array('product'=>$product));    
        } else { 
            throw new CHttpException(404,'Страница ошибки 404'); 
        }          
        Yii::app()->end();        
    }

It is very convenient to connect the main scripts of the site in the controller, there is no need to write sets of packages that are used throughout the site in each layout.
1. Is it possible in a certain Action to disable any packages of choice specified in beforeAction ?
2. If it is impossible to fulfill 1 point, is it possible to restrict the use of packages in the Action of the same controller responsible for Ajax data loading. It is very inconvenient when, after ajax loading of data, all packages specified in beforeAction are loaded again.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vyachin, 2015-06-04
@vyachin

Since you did it, you can't do it. All packages must be connected explicitly!!!! in the header of the template and only those that are necessary for this template. If you have a package (s) that are used in several templates - make your own layout for them, inherit from the main one and include common packages already in it.
If you return the result of an ajax request - use renderPartial, the last parameter will help you disable the transfer of css + js

I
Ilya, 2015-06-04
@flammerman

In layout, I did not connect any additional js / css scripts / styles at all - I included all scripts in the previous version of the application (before using beforeAction) in the corresponding view. In the layout, I connect only statics, something that will never change in the course of work. For example, the site menu, search bar, site footer and the like, I hope I'm doing it right?
In theory, I wanted to use the following structure: the "theme" script package is used on the entire site except for ajax uploads, the "cart" package is also needed on the entire site, working with the cart. Connect the rest as needed. But of course there are visibility problems, for example, "cart" is needed on the list of products, and at the same time inside the content of the modal window loaded via ajax, moreover, on the same page. Consider, on the element of the list of goods there is a button "add to cart" and in the modal window there is the same button in terms of functionality. And both of them pass product data to the controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question