How assets hepls to prevent resource dublication, when it used by many components?

Yii cookbook(russian) says, that assets hepls to prevent resource dublication, when it used by many components.

I dont understand how it helps to prevent resource dublication because when publishing assets full path to recource is used.

For example i wont to create few extensions(widgets). Each extension use the same script. Right way is to put js to widget_dir/views/assets and publish it. But when i will use both this extensions, script will be dublicated, beause we have 2 copies of this script: widget1_dir/views/assets/some_script.js and widget2_dir/views/assets/some_script.js

Anybody how to to solve this problem?

You should save the script in the same file, and include the same script in both widgets.

For example create a directory protected/scripts and save the common script here.

Then widget will be not universal and when i download some widget to protected/exensions/some_widget, i will need to care about placing js to corresponding directory. And many people use different directories to store their scripts. If somebody store his js in webApp/js or webApp/protected/js, it will ugly to create another dir for some scripts. Changing path to js in widget sources is bad practice, placing path to js to config is ugly to.

We need desigion, that let to use extesions from the box and don’t care about path to commonly used scripts and script dublication.

I don’t understand your point.

I guess that is a question of chouce: if you want an extension that will work out of the box, it should incorporate all needed script.

If I understand good, you want create 2 independent extension that will both work out of the box but which share some js script?

In this specific case you don’t need to publish the asset, is in the public directory.

Anyway that force people to move a file from extension to js, bad practice too.




I guess that is a question of chouce: if you want an extension that will work out of the box, it should incorporate all needed script.


If I understand good, you want create 2 independent extension that will both work out of the box but which share some js script? 



Yes, exacly. Extensions must incorporate all needed js and i need to avoid script dublication.

Questions is:

Standart Yii functionality can help to solve this problem?

What is most beautiful & universal way to do that?

It must be transparent for developers and html makeup

I think what you are trying to say and it is a good point -if it is what I think it is:

— if a widget has a jquery.fancybox.js and another has fancybox.js, both of them will register a duplicate of the same file.

Is that right?

If that is the case the programmer should be smart enough to find out those resources and if equal, ‘tweak’ the extension or the widget code to work accordingly to the application needs but… It could be a good point to work on common sense extension or widget convention or a publish function that will ‘automatically’ detects the javascript library. This is actually what I do.

But it could be great if there was an automated way (through the code) to publish external js libraries to the web/js/source folder and packages.php updated with new repository.

The extensions from jquery plugin are growing very fast in the community, if the problem you expose is actually true, then I think the way widgets or extensions created should review its conventions.

Antonio Ramirez

Yes, my english isn’t good, but you understand me correctly.

Oooh, I investigated CAssetManager and i think that $hashByName parameter of ‘publish()’ method is right that i need.

I am interested in your function, that ‘automatically’ detects the javascript library :)

Yes, i thought about that too. It’s good idea.

But better to place scripts for common use somwhere outside the framework directory and improve the registerCoreScript() methot to search JS’s in this dir.

Also would be nice to include many popular jQuery extensions such as jquery.validate, jquery.form to Yii core scripts.

I’m quite sure there was some discussion about this about 1-2 years ago. Unfortunately i can’t find that topic now. If i remember well there where concerns about dependency issues, like mismatch of versions etc. It’s not easy at all to find a generic solution for this (and that’s what you need if you want to publish something to the community).

Perhaps we should invent gii/widgetInstaller?

Edit: e.g add to application config, analyze the widgets for resources to be published from ext.widgetResources. If server space should be an issue, remove duplicate shared files.

/Tommy

I love this idea, the analysis should come by a convention (ie is not the same to include a compressed js file than a non compressed script).

Maybe a good option is to rewrite CClientScript? Maybe this component should be the one pre-analyzing the files. What do you think?

We could even add an option that those files should be downloaded (as an option) from google code with CGoogleApi (in many cases the response is faster to the client http://code.google.com/intl/es-ES/apis/libraries/) .

The idea of gii analyzing the files in the application is also really good. We could create some ‘js plugins and/or js libraries’ patterns for analyzing and search duplicated files, create a CClientScript configuration option (scriptMap) for config/main.php file and display also the differences between the files. Also timestamps, differences reports.

I am just dropping ideas.

Interesting thoughts.

For reference: here the old topics regarding similar issues i was talking about. Note that not everything said there is still valid. Those are 2 years old:

http://www.yiiframew…ic/38-guideline

http://www.yiiframew…cript-libraries

Is really good you share those links Mike.

I believe that the duplicated files could be solved (not in the way we were talking about that i think could be really cool) is to check which libraries the extensions are using and create a scriptMap manually.

Could be very, very good that a tool could analyze our site (as tri suggested) on the folders we specify and check the scripts used and build a scriptMap array in order to avoid duplicates.

mmm… or even build a component that is preloaded and analyzes, stores results in a cached source and is the scriptMap to be used afterwards…