开发者

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 开发者_Python百科leftovers of fighting crows
  • Archetypes widgets support old fashioned way to dump files directly to using macro slot fill

Any examples of existing implementations?


  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.


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.


Best practice is to have your javascript merged with as many other scripts as possible. Just load it either without a condition or only for authenticated users. All javascript code pertaining to editing included in Plone already loads for all authenticated users regardless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜