How to use javascript in Silverstripe CMS?

ぃ、小莉子 提交于 2019-12-23 20:59:28

问题


I'm using SilverStripe 3.0 CMS, and I need to include a Google Map into the CMS.

I'm following this steps, and besides it's a little bit old, the official documentation uses the same methods in the current version of SilverStripe (At least it seems to be the current version documentation).

The issue is in this part of the code:

Behaviour.register({ 
    "#Form_EditForm" : { 
        initialize : function() { 
            this.observeMethod("PageLoaded", this.adminPageHandler); 
            this.adminPageHandler(); 
        }, 
        adminPageHandler : function() { 
            initialize(); 
        } 
    } 
}); 

First of all, Behaviour was not defined. I needed to include manually the behaviour.js file that comes within the framework. But now, I get a Type Error:

this.observeMethod is not a function

Can someone give me a hint of what can I do in order to call a javascript function when a page editor is opened in the SilverStripe CMS?


回答1:


the 'Behaviour.register' call you mention is definitly deprecated and no longer available in the core code, so the docs need an update here.

unfortunately, i couldn't find a documented way to replace this behaviour, but for now the following should work for you, based on the approach in the forum post you mentioned first hand:

find the 'initGoogleMaps.js' script added here:

function getCMSFields() { 
Requirements::javascript('mysite/javascript/initGoogleMaps.js');  
...

inside this script, remove the Behaviour.register... block, and move the initialize function outside document.ready (or simply remove the document.ready part), so initialize is globally available (you might consider renaming it).

then, add the following inside getCMSFields:

$fields->addFieldToTab('Root.Content', new LiteralField('js', '<script>initialize();</script>'));

this will ensure the initialize function is called every time a page's 'edit view' is rendered inside the cms.

hth




回答2:


As mentioned by ben,

LeftAndMain::require_javascript('mysite/javascript/initGoogleMaps.js') 

is more reliable than 'include-it when needed'. Why? Because Silverstripe uses Ajax, it is better to load any javascript or css on the first load, so that they are ready when you go to different model admin areas within the CMS in ajax-powered environment. Not loading at the start causes inconsistency and your js, css files will not be loaded when you don't hard-load that admin area.

From documentation: http://doc.silverstripe.org/framework/en/reference/requirements and http://api.silverstripe.org/3.0/class-LeftAndMain.html

The whole "include it when you need it" thing shows some weaknesses in areas such as the CMS, where Ajax is used to load in large pieces of the application, which potentially require more CSS and JavaScript to be included. At this stage, the only workaround is to ensure that everything you might need is included on the first page-load.

One idea is to mention the CSS and JavaScript which should be included in the header of the Ajax response, so that the client can load up those scripts and stylesheets upon completion of the Ajax request. This could be coded quite cleanly, but for best results we'd want to extend prototype.js with our own changes to their Ajax system, so that every script had consistent support for this.

By the way, the ideal place for this line is _config.php in your custom module or in mysite, depending on your needs.




回答3:


LeftAndMain::require_javascript('mysite/javascript/initGoogleMaps.js') 

would work much better



来源:https://stackoverflow.com/questions/11994174/how-to-use-javascript-in-silverstripe-cms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!