1

I want to load the JS file in view to avoid heavy-loading of my application. I am using this JS file for CgrdView in view. SO i tried this:

Yii::app()->clientScript->registerScriptFile(
Yii::app()->baseUrl.'/assets/9d419b82/jquery.ba-bbq.js?v=1.0.127'
);

in My view header. But i am getting the error:Uncaught ReferenceError: jQuery is not defined As stated, i want to load jquery.ba-bbq.js file in view for CgriView, but i am not able to load that. Instead, i use following, but dont know is the efficient way to load or not.

<script src="/assets/9d419b82/jquery.ba-bbq.js?v=1.0.127" type="text/javascript"></script>

And is working when i load that in layouts, but this is not the efficient way.

FYI: I am working with cgridview search-box to post the search-box entry to my controller.

tnchalise
  • 1,573
  • 2
  • 18
  • 38
  • Don't put your custom files in the assets directory, that's for auto generated Yii files, it's much better to use your own directory (e.g. /webroot/js/jquery.ba-bbq.js?v=1.0.127). [Info Link](http://www.yiiframework.com/wiki/148/understanding-assets/#hh4) you might find useful. – Stu Jan 07 '13 at 11:56
  • Exactly, i want to autoload the js file by cgridview... – tnchalise Jan 07 '13 at 12:01
  • yii will autoload its own files when it needs them, you should never reference the assets directories? did yii create `jquery.ba-bbq.js`, or did you create that file? – Stu Jan 07 '13 at 12:02
  • I think, YII Owns jquery.ba-bbq.js. If not i dont know whats next.. suggest me..! Any help will be acceptable. – tnchalise Jan 07 '13 at 12:06
  • Doesn't CGridView load ba-bbq.js automagically? I'm not sure what you're trying to do manually here? – ernie Jan 07 '13 at 17:06
  • @ernie Is there any method to load that JS file in View without including that in layouts..? – tnchalise Jan 08 '13 at 04:00

1 Answers1

5

The bbq script is available as a core script and registering it will also load jquery.js. So to use bbq you simply need to do this:

Yii::app()->clientScript->registerCoreScript('bbq');

The assets folder is dynamic in nature, and it is used to store scripts, css, images in a web-accessible directory. It is automatically populated by the various Yii widgets, and also when you call registerCoreScript. Because of these reasons and more you shouldn't be relying on the assets folder to include js, css, images, but instead let the framework include it for you.


I couldn't find a list of the core packages in the docs, but it can be seen in the source file: framework/web/js/packages.php.


Incase you want this in a view with a CGridView, then let me tell you that CGridView loads this script automatically, and you don't have to register it again.

bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • I am confused little bit. Is the part where the **bbq** file is included is hidden in the view-page while we are viewing as page-sorce? I mean to say that, what if we want to see the page-source ? Is this really converted into ****?? Suggest me the real differences..! FYI: Your suggestion is not working for me.. that is still unable to load that file. – tnchalise Jan 08 '13 at 09:28
  • yes it should, i don't have the v= part though. can you show me your actual view file? and what exactly do you mean by real difference, and difference between what? – bool.dev Jan 08 '13 at 09:40
  • Is **Yii::app()->clientScript->registerCoreScript('bbq');** === **** ? – tnchalise Jan 08 '13 at 09:44
  • no `Yii::app()->clientScript->registerCoreScript('bbq');` will probably get you something like ``. i.e. `somerandomstring` can be anything, it could be 9d419b82 or something else. can you tell me again, do you already have a gridview in this view? – bool.dev Jan 08 '13 at 10:00
  • Yes, i have a **CGridView** in My view file, that has some columns and a row with search-box for each of the column, when i write **** in my view file, its working. But when i use **Yii::app()->clientScript->registerCoreScript('bbq');** or something, is not working. I dont actually know where i am stacked. – tnchalise Jan 08 '13 at 10:06
  • ok, have you tried not adding the js in layout/view, and only defining the gridview? because as has already been said gridview loads bbq by itself, so you don't have to include bbq again – bool.dev Jan 08 '13 at 10:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22377/discussion-between-tnc-and-bool-dev) – tnchalise Jan 08 '13 at 10:18