Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Requirements::javascript in CMS


Go to End


3 Posts   2986 Views

Avatar
micahsheets

Community Member, 165 Posts

27 June 2009 at 5:56am

I have a javascript requirment inside the getCMSFields function of my DataObject. I understand that in SS 2.3.2, this javascript should load when getCMSFields is called. However if I use ModelAdmin to search for the DataObject, once I have the EditForm loaded into the CMS I find that I have to reload the entire page to get the javascript to work. Why is that and is there a workaround or fix?

Avatar
Sam

Administrator, 690 Posts

27 June 2009 at 11:33am

Are you using Requirements::javascript() or Requirements::customScript()? Only Requirements::javascript() are loaded on demand by the ajax requests.

If you are using Requirements::javascript(), then you're going to have to debug it. Here are some tips:

* Double-check that your getCMSFields() method is actually being called, for example by putting a Debug::message() call into it. If it's not being called, then you've probably put your getCMSFields() method in the wrong place.
* Check the response headers of the ajax response - look at the X-Include-JS head. Does it list your file? If not, but your Requirements::javascript() call was being called, then there's a bug in Requirements.php
* Add an alert() method to the beginning of your javascript file to see if it's being loaded but is breaking for some other reason. Also, watch for parse errors loading the file. It could be that the JS on demand code is trying to load your script but is failing.
* If it looks like the file is being listed in X-Include-JS but isn't being loaded, then you might want to see if jquery.ondemand.js is actually loading the javascript
- Open sapphire/javascript/core/jquery.ondemand.js
- Find the start of the requireJs method, about line 46.
- Add console.log("Loading script: " + scriptUrl); at the beginning of the file.
- Is Loading script: (yourscript) getting listed in the Firebug console? If not, then the ondemand handler isn't loading your script for some reason and there's probably a bug in the ondemand handler.

I hope this helps!

Avatar
micahsheets

Community Member, 165 Posts

30 June 2009 at 8:53am

I can see that the ondemand handler is loading the script and the script is running. However if I have something like:

jQuery('#elementID').change(function(event) {}

it will not work but if I do this:

elementID = document.getElementById('ID');
jQuery(elementID).change(function(event) {}

The change event on elementID object works but inside the callback the "this" variable doesn't hold anything.

I tried putting the Requirement before the parent::getCMSFields and after but that doesn't make any difference.

My problem might be in my jQuery and not the ondemand handler.