Jump to:

23581 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Requirements::javascript in CMS

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

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

Page: 1
Go to End
Author Topic: 2129 Views
  • micahsheets
    Community Member
    164 Posts

    Requirements::javascript in CMS Link to this post

    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?

  • Sam
    679 Posts

    Re: Requirements::javascript in CMS Link to this post

    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!

  • micahsheets
    Community Member
    164 Posts

    Re: Requirements::javascript in CMS Link to this post

    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.

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.