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.

6
  • 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 you might find useful. Commented Jan 7, 2013 at 11:56
  • Exactly, i want to autoload the js file by cgridview... Commented Jan 7, 2013 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? Commented Jan 7, 2013 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. Commented Jan 7, 2013 at 12:06
  • Doesn't CGridView load ba-bbq.js automagically? I'm not sure what you're trying to do manually here? Commented Jan 7, 2013 at 17:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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 <script src="/assets/9d419b82/jquery.ba-bbq.js?v=1.0.127" type="text/javascript"></script>?? Suggest me the real differences..! FYI: Your suggestion is not working for me.. that is still unable to load that file.
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?
Is Yii::app()->clientScript->registerCoreScript('bbq'); === <script src="/assets/9d419b82/jquery.ba-bbq.js?v=1.0.127" type="text/javascript"></script> ?
no Yii::app()->clientScript->registerCoreScript('bbq'); will probably get you something like <script src="/assets/somerandomstring/jquery.ba-bbq.js" type="text/javascript"></script>. 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?
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 <script src="/assets/9d419b82/jquery.ba-bbq.js?v=1.0.127" type="text/javascript"></script> 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.