using Javascript with eZ

Author Message

Claudia Kosny

Friday 05 January 2007 3:46:58 am

Hi there

I am looking for tips on how to use Javascript with eZ. I have two, pretty similar, problems:

I use Ajax to reload some parts of a page. To do this I need the url of the server script that is to be called by the Ajax request. The problem is that if I follow the motto of keeping design and functionality apart, my whole Javascript is in an external js file. But in this file I do not have the option to call ezurl() to create a full url, so I have to hardcode the url which can be quite annoying if you move servers. Another option would be to add a script block in the pagelayout.tpl where I add these urls as global variables, so the scripts in the external files can see them always. But this quite error prone as the code for executing one function is separated in several places and if I have lots of scripts, this gets very confusing.

Has anyone a better idea how to solve this?

A similar problem is having messages in the Javascript. As I cannot call the i18n operator, I cannot translate the messages properly.

Any ideas?

Claudia

Betsy Gamrat

Tuesday 09 January 2007 8:38:37 pm

Claudia,

I would recommend adding an init or config function in the external js file. That function would be called by script code in pagelayout.tpl that would set the variables you need.

Good luck,

Betsy

For example:

<b>pagelayout.tpl</b>

<script src="{'ajax.js'|ezdesign}"></script>
<script>init_ajax('{$node.url_alias|ezurl}',30)</script>

<b>ajax.js</b>

target_url=''
timeout=0

function init_ajax(in_url,timelimit)
{
  target_url=in_url
  timeout=timelimit
}

function do_ajax()
{
  sendRequest(target_url)  /* Sample code, may not be correct syntax/call */
}

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 20 2025 16:47:20
Script start
Timing: Jan 20 2025 16:47:20
Module start 'layout'
Timing: Jan 20 2025 16:47:20
Module start 'content'
Timing: Jan 20 2025 16:47:20
Module end 'content'
Timing: Jan 20 2025 16:47:20
Script end

Main resources:

Total runtime0.0145 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0056 589.0469151.2109
Module start 'layout' 0.00560.0020 740.257836.6484
Module start 'content' 0.00760.0054 776.906390.1719
Module end 'content' 0.01300.0014 867.078129.9922
Script end 0.0145  897.0703 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002114.5251140.0002
Check MTime0.00106.7490140.0001
Mysql Total
Database connection0.002013.759710.0020
Mysqli_queries0.001913.056830.0006
Looping result0.00000.090510.0000
Template Total0.00117.510.0011
Template load0.00085.730010.0008
Template processing0.00031.759710.0003
Override
Cache load0.00063.914410.0006
General
dbfile0.00021.343280.0000
String conversion0.00000.042840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs