KJedi
(Konstantin Mirin)
March 3, 2009, 12:14pm
1
How about creating a method in the CClientScript, that registers the whole directory:
public function registerScriptDirectory($dir, $makeAsset = true)
{
//loop through files, applu CHtmp::asset to them if $makeAsset == true
}
This should be very convenient, I think…
Alternative approach to the batch inclusion is passing an array of files and base bath (where they are) there.
qiang
(Qiang Xue)
March 3, 2009, 12:19pm
2
Not sure if this is a good idea, but this is very costly because it involves traversing the directory. If you really want it, you may extend CClientScript and replace the existing "clientScript" component.
KJedi
(Konstantin Mirin)
March 3, 2009, 1:49pm
3
Thanks! Solved it and described solution in details here: http://programmersno…s-files-in-yii/
Maxximus
(Maxximus007)
March 3, 2009, 3:50pm
5
Yeah can be nice, and nicely written. You might want to cache the result for a while instead of pushing it all to registerScriptFile() in the loop.
KJedi
(Konstantin Mirin)
March 3, 2009, 4:40pm
6
Do you mean to form a new file from all that? Or how can I do "registerAll"?
Maxximus
(Maxximus007)
March 3, 2009, 5:01pm
7
Well, that's a nice option as well, you could take a look at jsmin and cssmin for that. But I meant something like this:
public function registerScriptDirectory($path, $makeAssets = true, $position = self::POS_HEAD)
{
$id = md5(serialize($path.$makeAssets.$position));
$data = Yii::app()->cache->get($id);
if($data === false)
{
$di = new DirectoryIterator($path);
foreach ($di as $file)
{
if (!$file->isDot() && !$file->isDir())
{
$data[] = $makeAssets ? CHtml::asset($path.$file->getFileName()) : $path.$file->getFileName();
}
}
Yii::app()->cache->set($id, $data, Yii::app()->params->cacheTime);
}
foreach ($data as $file)
{
$this->registerScriptFile($file, $position);
}
}
KJedi
(Konstantin Mirin)
March 3, 2009, 5:40pm
8
Thanks! Tested this, it works great I’ve updated the blog post.
Maxximus
(Maxximus007)
March 3, 2009, 5:49pm
9
Cool! Saw a new feature in CClientScript: scriptMap. Not sure yet how to use it, but the description sounds interesting…
KJedi
(Konstantin Mirin)
March 3, 2009, 5:51pm
10
yes, I saw that, but I also don’t know see how I can use it
Maxximus
(Maxximus007)
March 3, 2009, 11:40pm
11
scriptMap works the other way around, but a couple of the 1.0.3 functios in CClientScript will give some nice options. Will rework my JSmin and CSSmin lib to work a bit closer with these additions and share it.
qiang
(Qiang Xue)
March 4, 2009, 1:09am
12
scriptMap is described in detail here:
http://www.yiiframew…ics.performance
Maxximus
(Maxximus007)
March 6, 2009, 5:12pm
13
@Konstantin : You might want to take a look at http://www.yiiframew…opic,988.0.html . Perhaps there is some for you to combine it with your solution.
KJedi
(Konstantin Mirin)
March 6, 2009, 5:18pm
14
Thanks! However, I can’t do it now - urgent project, but I’ve planned this for later. If you do it before me (I’ll have time next weekends) - let me know, I’m interested in using such thing
Maxximus
(Maxximus007)
March 12, 2009, 8:21am
15
Konstantin: I made an extension: ExtendedClientScript . You can easily extend that one with yours, and (hopefully) take advantage of the compression options.
KJedi
(Konstantin Mirin)
March 12, 2009, 4:07pm
16
Thanks for letting me know, I'll test it these weekends.