Getting Started with Dojo Toolkit

23
Getting started with Dojo Toolkit Cologne.js User Group Meeting, Cologne (@cowoco) Thomas Koch (@tomy_koch) 10. August 2010

description

Slides from talk "Getting Started with Dojo Toolkit" at Cologne.JS in August 2010 some sample Dojo Code from my presentation is on http://code.google.com/p/dojodemo/ Cologne.JS is a local JavaScript user group: http://colognejs.de/

Transcript of Getting Started with Dojo Toolkit

Page 1: Getting Started with Dojo Toolkit

Getting started with Dojo Toolkit

Cologne.jsUser Group Meeting, Cologne (@cowoco)

Thomas Koch(@tomy_koch)

10. August 2010

Page 2: Getting Started with Dojo Toolkit

Dojo Toolkit: About

• Dojo is JS Framework– Pure client-side JS (in contrast to GWT etc.)

– Includes Dev.Tools: build, test

– Open source, active communityhttp://www.dojotoolkit.org/

• Dojo features– OO-style class based development

– Modularisation (via concept of packages)

– Support for data and GUI MVC

– Wide range of builtin widgets

10.08.2010 Thomas Koch 2

Page 3: Getting Started with Dojo Toolkit

Dojo Toolkit: Features

• Development: APIs for robust Web Applications– Event handling, DOM manipulation– CSS Querying, Animations, …

• Communication: Ajax and even more– Data abstraction layer, JSON-RPC, Comet, XMPP etc.

• Integration: with many server-side developments– including DWR, Django, TurboGears, Google, Jaxer …

• User interface: re-usable gui components– Standard gui widgets (button, slider, table, tree, menu …)– vector graphics, charting, …– theming, drag’n drop, …– i18N, a11y

• Widely used: Dojo Widgets (aka Dijits)

10.08.2010 Thomas Koch 3

Page 4: Getting Started with Dojo Toolkit

Dojo Sneak Preview: Theme Tester

10.08.2010 Thomas Koch 4

Page 5: Getting Started with Dojo Toolkit

GETTING STARTEDDojo quick-start

10.08.2010 Thomas Koch 5

Page 6: Getting Started with Dojo Toolkit

Basic Dojo Page

• minimal Dojo

– load the dojo base stuff: dojo.js

– define a config (optional!)

• djConfig : dictionary used by dojo bootstrap process– Locale, debug, baseURL, parseOnLoad, …

• Example

10.08.2010

<html><head><title>Dojo Demo</title>

<script type="text/javascript" src="dojo/dojo.js"

djConfig="isDebug:true">

</script></head>

<body><p>Hello Dojo</p></body>

</html>

Thomas Koch 6

Page 7: Getting Started with Dojo Toolkit

Document Life-Cycle

• OnLoad/unLoad Handler

– dojo.addOnLoad(<functionname>)

• defers script execution until all HTML is loaded

– vice versa: addOnUnload()

• Example

10.08.2010

<script type="text/javascript">

function showVersion() {

var node = dojo.byId("demo");

node.innerHTML = "Dojo"+dojo.version;

}

dojo.addOnLoad(showVersion);

</script>

<body>

<div id="demo"></div>

Thomas Koch 7

Page 8: Getting Started with Dojo Toolkit

Dojo Base

• Foundation of Dojo Framework: dojo.js– Implements Browser detection & Package loading

• import/packaging-concept via dojo.declare / dojo.require

– Provides DOM Manipulation and CSS query engine

• Browser sniffing– dojo.isFF, dojo.isIE

– dojo.isOpera, dojo.isSafari

– dojo.isAir

• Dojo.is<Browser> (global variable!)– IS version of browser

– OR undefined>>> dojo.isFF

3.510.08.2010 Thomas Koch 8

Page 9: Getting Started with Dojo Toolkit

DOM Manipulation

• DOM Lookup– node = dojo.byId("id") returns a DOM node

• Similar to document.getElementById("id")

– Watch out: dijit.byId("id") returns a Dijit widget instance!

• DOM Insert/Edit– dojo.create() & dojo.place()

var img = dojo.create("img"‚ ,src:"a.gif", alt:"demo" });

dojo.place(img, node, "after");

OR: dojo.create("img"‚ ,src:"a.gif", alt:"demo" }, node, "after");

• Dom Manipulation: cleanup– dojo.destroy(node)

– dojo.empty(node)

10.08.2010 Thomas Koch 9

Page 10: Getting Started with Dojo Toolkit

Dojo Base : Querying• CSS query engine

– dojo.query("img") returns NodeList• Similar to document.getElementsByTagName("IMG");

– nodelist = dojo.query(String query, String?|Node? root)• Query: CSS3-Selektor http://www.w3.org/TR/css3-selectors

– Examples• dojo.query(".progressIndicator"); // Query by css class.

• dojo.query(„div#demo"); // Query by node type and id

• dojo.query(„a*href*=‚www.bscw.de‘+"); // Query attributes

• NodeList– Subclass of Array with special extensions:

• every, forEach ,filter, concat, splice, addClass,removeClass…

10.08.2010 Thomas Koch 10

Page 11: Getting Started with Dojo Toolkit

Dojo Base : CSS

• CSS styling– Assign CSS classes

• dojo.addClass(node,clsStr) , removeClass, toggleClass, hasClass

– Assign CSS styles• dojo.style(DOMnode|String node, String|Object style, String val)

• Examplevar box = dojo.byId(„bannerTop“);

dojo.style(box, {height : „100px“, width: „200px“-);

<= => dojo.style(„bannerTop“, …);

• dojo.attr(DOMnode|String node, String name, String value)

• Examplevar img = dojo.byId(„logo1“);

dojo.attr(img, „alt“, „OrbiTeam“);

10.08.2010 Thomas Koch 11

Page 12: Getting Started with Dojo Toolkit

PRACTICAL EXAMPLEDojo hands-on

10.08.2010 Thomas Koch 12

Page 13: Getting Started with Dojo Toolkit

Examples - code available athttp://code.google.com/p/dojodemo/• 1. Simple dojo page

– Browser sniffing– DOM Manipulation– CSS Querying– onLoad-Handler

• 2. Use of dijits– dojo.require– dojoType magic– Dijit properties– Data layer

• 3. Define your own dijit– dojo.provide / dojo.declare– dijit.Widget / dijit.Templated

10.08.2010 Thomas Koch 13

Page 14: Getting Started with Dojo Toolkit

DOJO BUILDING BLOCKSThe big picture

10.08.2010 Thomas Koch 14

Page 15: Getting Started with Dojo Toolkit

Dojo Architecture

• Dojo consists of …– Base

• Foundation of everything else: packaging, loading, DOM manipulation

– Core• Widget parser, animations,

drag‘n drop, i18n …

– Dijit• Dijits = Dojo Widgets:

portable widgets(progress bar, menu, editor, …)

– DojoX• Dojo eXtensions: stable and

experimental widgets(like Charting, Grid, FishEye)

– Util• Dojo Build System, ShrinkSafe,

Checkstyle, API Doc System

10.08.2010 Thomas Koch 15

Page 16: Getting Started with Dojo Toolkit

Dojo Base

• Basic Toolset– Array Utilities (dojo.filter, dojo.forEach …)

– Language Utilities (dojo.hitch, dojo.isString …)

– OO Utilities (declare, mixin, extend ...)

– Event-System (publish, subscribe)

– Ajax / IO (dojo.xhr)

• Furthermore …– Animations

– String Utilities, JSON Tools (fromJSON, toJSON, …)

– Misc: Colors,Keys, global, eval

10.08.2010 Thomas Koch 16

Page 17: Getting Started with Dojo Toolkit

Dojo Core• Extends Dojo Base Framework

– Contains everything from dojo-Namespacethat needs import, e.g. dojo.require(„dojo.date“)

• Important Components– dojo.dnd: Drag and Drop– dojo.i18n: Internationalization Utility– dojo.rpc: Remote Procedure Calls with Backend Servers– dojo.data: a uniform data access layer

• Furthermore contains…– dojo.fx: Effects library on top of Base animations– dojo.gears: Google Gears– dojo.io: Additional AJAX I/O transports– dojo.html: Inserting contents in HTML nodes – Misc: dojo.currency, dojo.date, dojo.number, dojo.regexp etc.

10.08.2010Thomas Koch 17

Page 18: Getting Started with Dojo Toolkit

Dojo Unified Events

• Publish/Subscribe communication

• connect a function of your own to:– a DOM event, such as when a link is clicked

– an event of an object, such as an animation starting

– a topic, which other objects can publish objects to

– function call of your own, such as bar();

• Methods– dojo.[dis]connect(), dojo.publish, dojo.subscribe()

• Example

10.08.2010

var foo = dojo.byId("foo"); // anchor element

dojo.connect(foo, "onclick", function(evt) {

console.log("anchor clicked");

dojo.stopEvent(evt); //suppress navigation

}); Thomas Koch 18

Page 19: Getting Started with Dojo Toolkit

DOJO RELATEDFurther Information on Dojo

10.08.2010 Thomas Koch 19

Page 20: Getting Started with Dojo Toolkit

Dojo Documentation

• API Docs– Online Documentation : http://dojotoolkit.org/api/

• Dojo Campus– Articles, Tutorials, Feature Explorer … http://dojocampus.org

• Blogs– Dojo Project Blog

• http://dojotoolkit.org/blog/

– SitePen Blog• http://www.sitepen.com/blog

– Shane O’Sullivan’s technical blog• http://shaneosullivan.wordpress.com/

• Books– Russell, Matthew A.

• Dojo: The Definitive Guide

– Riecke, C.; Gill, R.; Russell, A.• Mastering Dojo:

– Zammetti, Frank• Practical Dojo Projects

10.08.2010 Thomas Koch 20

Page 21: Getting Started with Dojo Toolkit

Dojo Performance

• Dojo Performs Better (DOM Manipulation!)– Dojo is 1.5-2x faster than jQuery, and the difference is

biggest on the slowest browsers — where it counts most.

siehe TaskSpeed

– http://dante.dojotoolkit.org/taskspeed/

10.08.2010 Thomas Koch 21

Page 22: Getting Started with Dojo Toolkit

Dojo Foundation– http://www.dojofoundation.org/

– home of great open-source projects

– started as the home of the Dojo Toolkit

– today the Dojo Foundationis also home of other open web projects including cometD, DWR, OpenRecord, Persevere, and Sizzle.

– Dojo Foundation Board

– vote per committer

10.08.2010 Thomas Koch 22

Page 23: Getting Started with Dojo Toolkit

Dojo Demos

• Demo #1– Dojo Image Demo: Zoomer

– http://demos.dojotoolkit.org/demos/cropper/

• Demo #2– Dojo Feature Explorer

– http://dojocampus.org/explorer

• Demo #3– Sitepen Persevere Demo: Stocker

– http://persevere.sitepen.com/stocker.html

10.08.2010 Thomas Koch 23