CartoWeb 3.4.0
Download Now!


CartoWeb
Advanced Geographical Information System for the Web

How to extend Cartoweb core javascript

The easy and ugly way is to copy the .js file(s) in your project and modify it. It works but you will bang you head on the wall the day you want to upgrade Cartoweb to a newer version as you will find yourself with probably incompatible .js file(s) with modified or missing parts.

There is fortunately a proper way and it's surprisingly relatively easy :

The idea is to override ONLY the function(s) you want to modify and put them in a new .js file. Simply load that .js file AFTER the main dhtml javscript inclusion in the header.

Example:

Let say we want to modify the effect of the onClic event for the Map.prototype.surface object. (In other words, what happens when we clic on the map with the Surface tool)

1

create a new empty .js file and put inside :

Map.prototype.surface = function(aDisplay) {

   // here write your modified code

}

This will replace all the code corresponding to the existing Map.prototype.surface object defined in dhtmlInit.js

Note that if you only want to override one of the property of the object, you still need to override the whole object definition.

2

save the file in your project's /htdocs/js/ folder

3

add

<script type="text/javascript" src="{r type=js}your_new_js_file.js{/r}">

at the end of the head of your project's cartoclient.tpl, AFTER the dhtmlcode.tpl (or cartoclient_ajaxHeader.tpl if you are overriding a Cartoweb's ajax object) :

<head>

...

  {include file="cartoclient_ajaxHeader.tpl"}
  
  {include file="dhtmlcode.tpl"}

...

   <script type="text/javascript" src="{r type=js}your_new_js_file.js{/r}">

</head>

4

run the cw3setup.php script with the --install option so the new js file is taken into account.

conclusion

This way you have effectively overrided a function without touching or duplicating all the javascript of Cartoweb.

When you upgrade to a newer version of cartoweb, the core javascript files of Cartoweb will be up-to-date and you have minimised the risk of conflict. I say "minimized" and not "avoided" because you cant exclude the new version of the files doesnt modify something in the very same piece of code you overrided, but the risk is low and new version usually simply add new function. Modification only concern bug fix usually as we try to avoid as much as possible changes that arent backward compatible.

CartoWeb Wiki: HowToCorrectlyOverrideJavascript (last edited 2020-05-23 07:37:00 by OliverChristen)

© 2002-2007 CartoWeb by Camptocamp SA - Wiki powered by MoinMoin