Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering a Javascript to be loaded on edit view

What is the recommended way to register a Javascript to be loaded on the edit view (Archetypes) of certain content types?

  • Javascript would be merged and compressed if it's portal_javascripts
  • Creating portal_javascript condition is a problem: how to create a condition line checking for edit views of a certain content types and so that the line doesn't look like the leftovers of fighting crows
  • Archetypes widgets support old fashioned way to dump files directly to using macro slot fill

Any examples of existing implementations?

like image 962
Mikko Ohtamaa Avatar asked Dec 17 '22 15:12

Mikko Ohtamaa


2 Answers

  1. Make sure your JavaScript code works OK in those content type edit forms and doesn't break on other situations (jQuery is your friend). Then add it to portal_javascripts.

  2. If you can associate the JavaScript to certain widgets, add it as helper_js for those widgets. Be careful with JavaScript as browser resources because I had trouble with '++resoure++'-like URLs as helper_js: apparently Archetypes doesn't like the plus (+) sign (doesn't escape plus sign correctly if I understand it OK).

  3. You can define a page template named archetypes_custom_js with a macro called javascript_head and it will automatically be loaded in edit form of all Archetypes. You can then check special conditions in that macro. See base_edit.cpt line 47.

like image 72
marcosfromero Avatar answered Dec 29 '22 15:12

marcosfromero


To be honest is such cases I like the approach that use old fill-slot directly in the edit form of the type. The edit form is often not-so-frequently called (and for sure not from anonymous) so a little overhead due to downloading not-fully cached code is not so bad.

like image 25
keul Avatar answered Dec 29 '22 15:12

keul