Copyright © 2011, Zeleos Project Team
Zeleos Web Toolkit main component is the JavaScript API that was created following strict coding conventions.
JavaScript wasn't meant to be used for complex UI development. It was designed to add some limited dynamic behavior in a web page. It is basically very useful for Form validation. Today we are using it into more complex UI frameworks. This is only possible if we define strict coding conventions.
ZWT is using most of the conventions used in Java:
org.zeleos.Test
must be placed here org/zeleos/Test.js
file).
However we cannot write JavaScript directly in Java, JavaScript does not understand encapsulation and it hardly handles inheritance. So we figured out a way to write correct JavaScript classes:
/* * Copyright 2011 Jeremy KUHN * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace('org.zeleos.template'); include('zwt.Object'); /** * Constructs an object. * @constructor * * @class This is a sample class that follows ZWT coding convention. * @extends zwt.Object * * @author Jeremy KUHN */ org.zeleos.template.MyClass = function(string_field) { org.zeleos.template.MyClass.superClass.constructor.call(this); this.field = string_field; }; org.zeleos.template.MyClass.inherits(org.zeleos.template.ParentClass); /** * The foo method. */ org.zeleos.template.MyClass.prototype.foo = function() { org.zeleos.template.MyClass.superClass.prototype.foo.call(this); alert('foo'); }; /** * @return the field attribute. * @type string */ org.zeleos.template.MyClass.prototype.getField = function() { return this.field; }; /** * Set the field attribute * @param {String} string_field The field to set. */ org.zeleos.template.MyClass.prototype.setField = function(string_field) { this.field = string_field; };
Licensing information appear first in the file.
Note the use of the namespace
method which creates the namespace of the class.
You must use the include
method to import (and load) other classes used within the class. These two methods are provided by the ZWT library.
The inherits
method lets you extends a base class. In this example, we specify that the org.zeleos.template.MyClass
class extends the org.zeleos.template.ParentClass
class : org.zeleos.template.MyClass.inherits(zwt.
Object);
.
Note also how the constructor of the base class is called using JavaScript call method : org.zeleos.template.MyClass.superClass.constructor.call()
.
We can also notice that the class override the foo
method as it calls the parent foo
method: org.zeleos.template.MyClass.superClass.
prototype.foo.call(
this);
.