JavaScript files naming convention

486 views
Skip to first unread message

Robert Impey

unread,
Mar 11, 2010, 11:05:50 AM3/11/10
to Haddock CMS
I've noticed the addition of some new JavaScript files in the Video
Library plug-in.

http://haddock-cms.googlecode.com/svn/plug-ins/video-library/trunk/public-html/scripts/VideoLibrary_ManageExternalVideosPage.js
http://haddock-cms.googlecode.com/svn/plug-ins/video-library/trunk/public-html/scripts/VideoLibrary_HTMLPage.js

The names of the files are similar to the names of the PHP classes.
However, the code in the files appears to be procedural. This might
become confusing. A JavaScript file full of functions should probably
be called something like 'foo-functions.js'.

Would FooBar_FeeFyFoh.js only be used by the FooBar_FeeFyFoh class
defined in FooBar_FeeFyFoh.inc.php?

Saul Howard

unread,
Mar 12, 2010, 12:53:06 AM3/12/10
to Haddock CMS
You are correct, the JS files do just contain procedural code, and the
names I've given them are bad.

The reason was because in practice, each Foo_HTMLPage class uses
exactly one JS file. Using JQuery's:

$(function() {
// Code
});

So that the code is executed on page load.

If I'm using JS, I've found that nearly every single HTML Page Class
needs a corresponding JS file containing this JQuery function. I can
then also factor out common functions into a foo-functions.js, but
there is a definite need for one of these JS files for each Page
Class.

What then should the naming convention be? Something that explicitly
relates the JS file to FooBar_FeePage, but I agree that giving it the
_same name_ is ugly and wrong.

An alternative could be to include the JQuery $(); function in the
HTML body, within script tags. Then all external JS files could just
contain helper functions. This actually sounds quite neat as it would
save flipping between the JS and the Page Class.

Any ideas?

Saul


On Mar 11, 11:05 pm, Robert Impey <robert.im...@gmail.com> wrote:
> I've noticed the addition of some new JavaScript files in the Video
> Library plug-in.
>

> http://haddock-cms.googlecode.com/svn/plug-ins/video-library/trunk/pu...http://haddock-cms.googlecode.com/svn/plug-ins/video-library/trunk/pu...

Robert Impey

unread,
Mar 12, 2010, 4:57:44 AM3/12/10
to haddo...@googlegroups.com
On 12 March 2010 05:53, Saul Howard <saulh...@gmail.com> wrote:
> You are correct, the JS files do just contain procedural code, and the
> names I've given them are bad.
>
> The reason was because in practice, each Foo_HTMLPage class uses
> exactly one JS file. Using JQuery's:
>
> $(function() {
> // Code
> });
>
> So that the code is executed on page load.
>

As a side note, I've written a plug-in for Haddock that just contains
JS for running functions on load, which is based on code from page 434
of "JavaScript: The Definitive Guide" by David Flanagan (5th Edition).

http://haddock-cms.googlecode.com/svn/plug-ins/run-on-load/trunk/

You need the script, e.g.

http://programming.impey.info/plug-ins/run-on-load/public-html/scripts/run-on-load.js

You need to have a file like:

http://programming.impey.info/plug-ins/public-html-sky-theme/public-html/scripts/register-functions-to-run-on-load.js

With content like:

runOnLoad.funcs.push(round_all_bw_rc_corners);

You can add as many functions as you like to this, in several files.
The script builds a list of functions to be run and then runs them.

> If I'm using JS, I've found that nearly every single HTML Page Class
> needs a corresponding JS file containing this JQuery function. I can
> then also factor out common functions into a foo-functions.js, but
> there is a definite need for one of these JS files for each Page
> Class.
>
> What then should the naming convention be? Something that explicitly
> relates the JS file to FooBar_FeePage, but I agree that giving it the
> _same name_ is ugly and wrong.
>

I don't see much of a problem with it. A one to one relationship is
simply conveyed in this way. It might be a problem if we start writing
more object oriented JS (which is a bit odd, but possible, because of
the syntax of the language) and want to put class definitions in files
in this way.

> An alternative could be to include the JQuery $(); function in the
> HTML body, within script tags. Then all external JS files could just
> contain helper functions. This actually sounds quite neat as it would
> save flipping between the JS and the Page Class.
>

If it's just code that has to be run after the page has loaded, it's
probably a good design decision to keep the code for the specific
pages as short as possible (e.g. a one line function call) for
maintainability and code reuse.

> Any ideas?
>
> Saul
>
>
> On Mar 11, 11:05 pm, Robert Impey <robert.im...@gmail.com> wrote:
>> I've noticed the addition of some new JavaScript files in the Video
>> Library plug-in.
>>
>> http://haddock-cms.googlecode.com/svn/plug-ins/video-library/trunk/pu...http://haddock-cms.googlecode.com/svn/plug-ins/video-library/trunk/pu...
>>
>> The names of the files are similar to the names of the PHP classes.
>> However, the code in the files appears to be procedural. This might
>> become confusing. A JavaScript file full of functions should probably
>> be called something like 'foo-functions.js'.
>>
>> Would FooBar_FeeFyFoh.js only be used by the FooBar_FeeFyFoh class
>> defined in FooBar_FeeFyFoh.inc.php?
>

> --
> You received this message because you are subscribed to the Google Groups "Haddock CMS" group.
> To post to this group, send email to haddo...@googlegroups.com.
> To unsubscribe from this group, send email to haddock-cms...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/haddock-cms?hl=en.
>
>

--
Rob Impey

Reply all
Reply to author
Forward
0 new messages