dgDojoPlugin/DojoJavascript (diff)

You must first sign up to be able to contribute.

Changes between Version 1 and Version 2 of dgDojoPlugin/DojoJavascript

Dean.Glazeski (IP:
07/27/08 22:56:54 (9 years ago)



  • 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: 
    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] 
    99== Javascript Manager == 
     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.   
     13=== Creating a Javascript Manager === 
     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. 
     20$manager = JSManager::getManager('dojo'); 
     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. 
     26=== Adding Javascript to the Manager === 
     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. 
     33// get the default Javascript manager 
     34$manager = JSManager::getManager(); 
     35$manager->addScript('alert("Hello, user")'); 
     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. 
     41=== Retrieving the Javascript from a Manager === 
     43Getting the Javascript currently set up in a manager is simple.  All you need to do is the following. 
     48// get the default Javascript manager 
     49$manager = JSManager::getManager(); 
     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. 
     56=== Registering or Unregistering Variable Names === 
     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. 
     63// get the default Javascript manager 
     64$manager = JSManager::getManager(); 
     65$varName = $manager->registerVar('foo'); 
     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. 
     74// get the default Javascript manager 
     75$manager = JSManager::getManager(); 
     76// open up the foo variable name 
     78// register it again 
     79$var = $manager->registerVar('foo'); 
     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. 
     85== Javascript Variable == 
     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. 
     89=== Creating a Javascript Variable === 
     91The creation of a JSVar is simple.  You do the following. 
     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(); 
     105After that, you can use that variable to do tons of stuff. 
     107=== Using a JSVar to Do Javascript === 
     109Writing Javscript is now just as easy as calling functions and setting properties of a variable.  For example, 
     114// create a unique variable in the default manager 
     115$var3 = new JSVar(); 
     117// get the default Javascript manager 
     118$manager = JSManager::getManager(); 
     119$manager->addScript("var $var3 = dojo.byId('foo')"); 
     121// JS: unique.srcNode = 'foobar'; 
     122$var3->srcNode = 'foobar'; 
     124// JS: var unique1 =; 
     125// JS: unique1.display = 'none'; 
     126$var3->style->display = 'none'; 
     128// JS: var unique2 = unique.getChildren(); 
     129// This returns a JSVar, but we ignore it 
     134=== JSVar destruction === 
     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.