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.

Archive /

Our old forums are still available as a read-only archive.

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

javascript not evaluated in right-side CMS panel


Go to End


6 Posts   2117 Views

Avatar
karo

Community Member, 6 Posts

21 June 2008 at 2:06am

hi

I'm trying to add GoogleMaps feature to client's CMS.
I'm using both maps module and my own classes.

I have everything working, except one problem, which i cannot get around for days already.
For example, i'm successfully using GMapField class to add points to database. I'm using it in a DataObject pop-up window.

The problem is, i cannot make it work in normal CMS pane (Main tab).

When i (re)load the whole CMS, the map is there.
But when i click on (another) page in the tree on left-hand side, displaying another site "page" (in my case a wrapper for a map object), the google map doesn't show.
I realised the reason is that GMapField is a javascript-generated "field", and javascript doesn't get evaluated when right-hand pane (Main and other tabs) is reloaded. It is evaluated only when the whole CMS window is (re)loaded. In pop-up it works always because it's an iframe, with a freshly loaded document each time.

So i wrapped the map JS code into a function, and introduced a button to call it; in order to execute the loaded JS in right pane.

The result? I always get the same map, regardless of which map was clicked in the tree. And the map is the one which was active when CMS was last reloaded.

In other words, same sh*t - JS got evaluated (and variables assigned values) only on window load; not on right-pane refresh.
And JS in the refreshed pane is just dead text.

Can someone please help me solve this in any way?

Avatar
karo

Community Member, 6 Posts

21 June 2008 at 2:13am

NOTE:
at first i thought Ajax (via prototype.js) was used to load CMS's right pane; so i set evalScripts:true as a default in prototype's Ajax options object.

When it didn't help i found out it's not how it works. On CMS's site tree branches there's just normal A HREF tags.
But i see that although a link may be e.g. "...admin/show/16", the URL in browser stays the same as it was, and just the content pane is reloaded. Sounds like Ajax but it isn't.

If i would understand how this works, it might help me with my problem.

So any explanation on this mechanism is also greatly appreciated!

Avatar
karo

Community Member, 6 Posts

24 June 2008 at 1:22am

Anyone?

Please, i cannot get past this point.

I'm sure someone from the core team can briefly explain about JS in CMS tabs,
or at least about how the linking in the CMS site - tree works...

Thanx!

Avatar
Ingo

Forum Moderator, 801 Posts

28 June 2008 at 6:38pm

loading javascript behaviour via ajax is tricky - if you look at the GMapField.php class, it actually generates the javascript in the Field() method, which is bad practice.
http://open.silverstripe.com/browser/modules/maps/trunk/code/GMapField.php
the problem is that the <script> tag won't get evaluated by default (see LeftAndMain.js->loadNewPage() and successfullyReceivedPage())

the class should use Behaviour.js to register this initialization code (and get it reapplied after ajax loading).

including javascript libraries for formfields is a bit of a mess as well, have a look in LeftAndMain.php->init() - we can't lazy-load javascript files on demand, so we have to assume all possible includes. another area which needs improvement.

sorry that i can't provide a quick answer here, either way will involve some core changes - we're aware of the issues. you might want to contact the original module author for further help (the "maps" module wasn't developed by Silverstripe).

Avatar
dio5

Community Member, 501 Posts

25 October 2008 at 11:46am

Edited: 25/10/2008 11:47am

Basically running into the exact same problem: how do I get my js function to be evaluated again after the first load?

Not using a class, nor Behaviour - just a simple function (in ext .js file) and a little jquery in a formfield.

No simple way to get this pushed into the ajax callback so it will work after creating new pages or loading other ones?

So Ingo, you're saying that there's no way without hacking into the core?

Karo, how did you get around this?

Thanks

Avatar
dio5

Community Member, 501 Posts

27 October 2008 at 12:30am

Edited: 27/10/2008 12:36am

I've solved my problem by hacking LeftAndMain_right.js and adding the initiation of my function to the prepareForm() function at around line 13.

prepareForm : function() {
		ajaxActionsAtTop(this.id, 'form_actions_' + this.formName, this.formName);
		
                // here goes my function 
                myFunction();
	},

If anyone knows a better way...