How to use assets to centralize JS library

Hello,

I’d like to have a centralized location to place the JEditable library and call it from multiple views. I’m thinking the best way to do this is place in the assets folder and access from there but not 100% sure how to do this. I’ve checked other forum posts and tried an approach but have gotten it to work.

Here’s what I did:

1- Placed the library under extensions/assets/js

2- Called the library using:


Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.assets.js'));

3- Called the library using :


 Yii::app()->clientScript->registerScript("editable","$('.edit').editable(''http://localhost/yalla/index.php?r=timeentries/editfield', {

         indicator : 'Saving...',

         tooltip   : 'Click to edit...'

     });",CClientScript::POS_READY);




)

Does the above look like the right approach? Not sure why it didn’t work on the first attempt.

Hey Student,

You aren’t actually including anything in the html by calling publish, it just copies the files in ext.assets.js to the assets directory.

A more complete solution is:




$path = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.assets'));

//replace thejavascriptfile.js with the filename

Yii::app()->getClientScript()->registerScriptFile($path . '/js/thejavascriptfile.js');



And your third step remains unchanged.

Luke thank you. I will try it.