Development

dgDojoPlugin/DojoJavascript (diff)

You must first sign up to be able to contribute.

Changes between Version 1 and Version 2 of dgDojoPlugin/DojoJavascript

Show
Ignore:
Author:
Dean.Glazeski (IP: 68.225.171.15)
Timestamp:
07/27/08 22:56:54 (9 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • dgDojoPlugin/DojoJavascript

    v1 v2  
    33The main idea of this framework is to create a simple interface to create Javascript while still using a PHP interface.  This was accomplished by creating a few classes that help to manage Javascript functions, variables, and the script itself.  This classes include the following: 
    44 
    5  * [wiki:dgDojoPlugin/DojoJavascript#JSManager Javascript Manager] 
    6  * [wiki:dgDojoPlugin/DojoJavascript#JSVar Javascript Variable] 
    7  * [wiki:dgDojoPlugin/DojoJavascript#JSFunction Javascript Function] 
     5 * [wiki:dgDojoPlugin/DojoJavascript#JavascriptManager Javascript Manager] 
     6 * [wiki:dgDojoPlugin/DojoJavascript#JavascriptVariable Javascript Variable] 
     7 * [wiki:dgDojoPlugin/DojoJavascript#JavascriptFunction Javascript Function] 
    88 
    99== Javascript Manager == 
     10 
     11The purpose of this class is to maintain the Javascript that a user wishes to have executed and also maintains a registration of variables that are currently in use.  The class is non-static and allows you to have more than one manager to manage different sections of Javascript.   
     12 
     13=== Creating a Javascript Manager === 
     14 
     15The Javascript Manager does maintain a single static method to retrieve or create Javascript Managers.  For instance, to get the manager for the Dojo instance, one might do the following. 
     16 
     17{{{ 
     18#!php 
     19<?php 
     20$manager = JSManager::getManager('dojo'); 
     21?> 
     22}}} 
     23 
     24If the dojo instance does not exist, the manager will be created and registered within the JSManager's list of managers.  From here, all methods are non-static and only affect that instance of the class. 
     25 
     26=== Adding Javascript to the Manager === 
     27 
     28The Javascript manager allows you to easily add Javascript to be executed.  For instance, to have an alert be fired using Javascript, you could do the following. 
     29 
     30{{{ 
     31#!php 
     32<?php 
     33// get the default Javascript manager 
     34$manager = JSManager::getManager(); 
     35$manager->addScript('alert("Hello, user")'); 
     36?> 
     37}}} 
     38 
     39This will add the script to the manager.  Semicolons are automatically added to lines, so you don't need to worry about those.  With Javascript added, you now need to retrieve it. 
     40 
     41=== Retrieving the Javascript from a Manager === 
     42 
     43Getting the Javascript currently set up in a manager is simple.  All you need to do is the following. 
     44 
     45{{{ 
     46#!php 
     47<?php 
     48// get the default Javascript manager 
     49$manager = JSManager::getManager(); 
     50$manager->printScript(); 
     51?> 
     52}}} 
     53 
     54This will print the Javascript that has been registered with the default manager.  This script will be printed in Javascript tags to make this even easier. 
     55 
     56=== Registering or Unregistering Variable Names === 
     57 
     58The Javascript manager maintains an array of variable names that are currently in use in the Javascript.  In order to maintain the list, you must use the register variable functions.  For example, to register the variable 'foo', you would do the following. 
     59 
     60{{{ 
     61#!php 
     62<?php 
     63// get the default Javascript manager 
     64$manager = JSManager::getManager(); 
     65$varName = $manager->registerVar('foo'); 
     66?> 
     67}}} 
     68 
     69The registerVar will attempt to register the variable name and return the actual variable name.  The reason this is done is so that if the variable is already registered, the function will return the new variable name, namely the desired name with a number appended to it.  This helps to keep the variables unique.  If you want to reuse a variable name and still want to register it, you can do the following. 
     70 
     71{{{ 
     72#!php 
     73<?php 
     74// get the default Javascript manager 
     75$manager = JSManager::getManager(); 
     76// open up the foo variable name 
     77$manager->unregisterVar('foo'); 
     78// register it again 
     79$var = $manager->registerVar('foo'); 
     80?> 
     81}}} 
     82 
     83By doing this, you can be sure that you won't overwrite any variable you want to use.  The manager can be nice for managing Javascript, but there are two other classes that help out. 
     84 
     85== Javascript Variable == 
     86 
     87The Javascript variable automatically registers its name with the specified Javascript manager, default is the default manager.  It also adds some capabilities to really help with writing Javascript.  Once it has the manager, whenever you do anything with the JSVar, it will automatically add the Javascript equivalent to the manager. 
     88 
     89=== Creating a Javascript Variable === 
     90 
     91The creation of a JSVar is simple.  You do the following. 
     92 
     93{{{ 
     94#!php 
     95<?php 
     96// register foo as name using the myapp manager 
     97$var = new JSVar('foo', 'myapp'); 
     98// register bar as name using the default manager 
     99$var2 = new JSVar('bar'); 
     100// create a unique variable in the default manager 
     101$var3 = new JSVar(); 
     102?> 
     103}}} 
     104 
     105After that, you can use that variable to do tons of stuff. 
     106 
     107=== Using a JSVar to Do Javascript === 
     108 
     109Writing Javscript is now just as easy as calling functions and setting properties of a variable.  For example, 
     110 
     111{{{ 
     112#!php 
     113<?php 
     114// create a unique variable in the default manager 
     115$var3 = new JSVar(); 
     116 
     117// get the default Javascript manager 
     118$manager = JSManager::getManager(); 
     119$manager->addScript("var $var3 = dojo.byId('foo')"); 
     120 
     121// JS: unique.srcNode = 'foobar'; 
     122$var3->srcNode = 'foobar'; 
     123 
     124// JS: var unique1 = unique.style; 
     125// JS: unique1.display = 'none'; 
     126$var3->style->display = 'none'; 
     127 
     128// JS: var unique2 = unique.getChildren(); 
     129// This returns a JSVar, but we ignore it 
     130$var3->getChildren(); 
     131?> 
     132}}} 
     133 
     134=== JSVar destruction === 
     135 
     136Whenever the JSVar is destroyed, it automatically unregisters its variable name so that it can be used again.  So, when you call a function but don't capture its return variable, the variable name won't get used up.