~niv-openerp/+junk/webdoc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
JavaScript Basics
=================

Preamble
--------

What is a Web Application ?
"""""""""""""""""""""""""""

A web application is simply an application that is delivered and used through a web browser, but the term has recently taken
a more specific meaning.

The old way to make a web application, and the way OpenERP worked until version 6.0 is to make the server send to the
user complete HTML documents representing the state of the application's GUI. This means the server has to compute and
send a new HTML document for each interaction; buttons clicks, searches, history navigation all require the server to
resend a document.

This puts a large load on the server and thus severely impact the number of concurrent users that can be served. It also
creates a large latency in the application that makes the implementation of many features impossible, and limits what
can be achieved in terms of usability.

The solution is to create a complete and standalone application in JavaScript that runs on the user's web browser. That
type of application comes to have much more in common with traditional desktop applications (example: GTK, Swing, Qt,
Windows Forms,...) than PHP-like web sites. The only difference with desktop applications, besides the programming
language and libraries used, is that the web client is downloaded and run by the user's browser each time he visits the
OpenERP website.

A Note about JavaScript
"""""""""""""""""""""""

JavaScript is the language understood by web browsers and thus a de-facto language for web applications. If you want to
develop for OpenERP's Web client you'll need to know JavaScript.

Objectively, JavaScript is a not a good programming language. It was designed by Netscape in 1995 for commercial purpose
by a small team with a short development time. It was not meant to be the most universal programming language in the
History. It has a lot a initial design problems and, due to backward-compatibility necessity, it was not really improved
since its creation.

Additionally JavaScript suffers from its wide popularity. This results in a lot of Google search results about
JavaScript being articles written by people that do not have a formal training in computer programming or that can't
even program at all but still manage to get some effects by copy-pasting code.

Still, despite its problems, the core of the language contains good ideas allowing a lot of creativity for programmers.
(Like prototype-based programming and functional programming.) Most of JavaScript's shortcomings can be erased by using
the correct patterns and the correct tools. It also has strong advantages on its own. First of all, JavaScript is very
fast. Fast to start, fast to run and fast to deploy. The ability to use HTML and the multimedia API of the browsers also
allows to create very nice looking applications and good productivity compared to desktop application programming. The
decisive point is probably the fact that JavaScript virtual machines are available on 99.99% of the desktop computers on
the planet.

In the end, if you're a good programmers with the good libraries, the advantages far outweighs the inconveniences and
makes JavaScript and the browser one of the best environment to develop applications for the general public.

Command Line Interpreter
------------------------

To test the basic features of the language, we recommend you to begin by using a command line interpreter. Most
modern web browsers will provide a console to use JavaScript, but it is recommended to use `Google Chrome
<http://www.google.com/chrome/>`_ for OpenERP's web module development. All this guide will assume you this particular
browser.

Once Chrome is installed, open any web page then go in the configuration menu of Chrome and select *Tools > Developer
Tools* or use *Ctrl + Shift + I*. This should reveal a new section at the bottom of the window. Now select the
*Console* panel. You should have a screen looking like this:

.. image:: img/chrome.png
   :align: center
   :width: 70%

You will now be able to test the code snippets given in the next part.

Basic Data Types
----------------

Numbers
"""""""

::

    > ((3 + 3) / 1.5) * 2;
    8

Please note **JavaScript do not have integers**. All numbers are floats. This is a major difference with most other
programming languages. This has impacts on some mathematical operations. Example:

::

    > 3 / 2;
    1.5

In C, Java or Python the result would be 1, except if one of the members was casted to float or explicitly declared as
float using a different notation (``2.0`` or ``2.``).

Booleans
""""""""

::

    > 5 == 2;
    false
    > true == true;
    true
    > true == false;
    false

As simple as booleans can be.

Strings
"""""""

::

    > "Hello World";
    "Hello World"
    > 'Hello World';
    "Hello World"

Strings can be declared using single quotes or double quotes. Like most high level programming languages, strings have
methods for many operations.

::

    > "Hello World".charAt(3);
    "l" // like Python, there is not char type in JavaScript, a char is a string of size 1
    > "Hello World".slice(6, 9);
    "Wor" // slice() is used to obtain a sub-string
    > "Hello World".length;
    11

Strings also use the + operator for concatenation:

::

    > "Hello " + "World";
    "Hello World"

Null
""""

::

    > var x = null;
    > x;
    null

Similar to a lot of languages or ``None`` in Python.

Undefined
"""""""""

If you declare a variable but do not assign it it will have a special value. This value is not the same as ``null``.

::

    > var x;
    > x;
    undefined

Implicit Type Conversions
-------------------------

JavaScript provides automatic type conversion for most operators.

::
    
    > "Test" + 5;
    "Test5"

Practically that behavior can be useful in some cases, like implicit conversion to strings, but can also create strange
behaviors, the typical case being comparisons. Here are some examples:

::

    > "5" == 5;
    true
    > "" == 0;
    true
    > "0" == false;
    true

Like in C, numbers are considered as ``false`` if they are ``0`` and ``true`` otherwise. Strings are considered as
``true`` except if they are empty.

During any operations involving different types, multiple castings can occur which are quite complicated to predict for
the programmer. That's why it is considered safer to always use the ``===`` and ``!==`` operators.

::

    > "5" === 5;
    false
    > "" === 0;
    false
    > "0" === false;
    false

These operators will always return ``false`` if the types to compare are different.


Control Structures
------------------

JavaScript provides the same control structures than C. (To test with Chrome's console, you can use *Shift + Enter* to
enter multiple lines of code.)

::

    > if (true == true) {
          console.log("true is true");
      }
    true is true
    > var x = 0;
    > while (x < 3) {
          console.log(x);
          x++;
      }
    1
    2
    3
    > for (var i = 5; i < 8; i++) {
          console.log(i);
      }
    5
    6
    7

JavaScript also provides a specialized for structure to loop on objects. (``for (... in ...)``.) It should be noted
that, due to bad conception of the language involving variable scopes, functional programming, performances issues and
bad behavior with arrays, almost all experienced programmers will avoid the usage of that structure and rather use
functions provided by non-standard libraries like jQuery or Underscore. We recommend you to use ``_.each`` provided by
Underscore most of the time. (More information about that library is given later in this document.)

Functions
---------

Functions can be declared like this:

::

    > function talk() {
          console.log("Hello World");
      }
    > talk();
    Hello World

In JavaScript, functions are also a complete type by themselves. They can be declared as expressions and stored in
variables.

::

    > var talk = function() {
          console.log("Hello World");
      }
    > talk();
    Hello World
    > var talkAgain = talk;
    > talkAgain();
    Hello World
    > function executeFunc(func) {
          func();
      }
    > executeFunc(talk);
    Hello World

Function arguments are declared like in most languages, except without types. Please note that the JavaScript virtual
machine never checks the number of arguments when a function is called. If there are more arguments the function will be
called anyway, if there are fewer arguments the remaining ones will be ``undefined``.

::

    > var print = function(a, b) {
          console.log(a);
          console.log(b);
      }
    > print("hello");
    hello
    undefined
    > print("nice", "to", "meet", "you");
    nice
    to

Variables and Scopes
--------------------

A variable is declared by preceding its name by ``var``. Unlike C++ and Java, a scope is not defined by the existence of
braces. A scope is defined by a function.

::

    > function func1() {
          var x; // x is inside the scope of func1
          function func2() { // func2 is inside the scope of func1
              var y; // y is in the scope of func2
          }
          while (true) {
              var z; // z is not in a new scope, it is the same scope than x
          }
      }

In this example, ``z`` is not a variable which is re-created at each iteration of the ``while`` loop, it is always the
same variable for each iteration because the variable is defined in the scope of ``func1``.

Functions can also access the variables defined above them.

::

    > function func1() {
          var x = "hello";
          function func2() {
              console.log(x);
          }
          func2();
          x = "world";
          func2();
      }
    > func1();
    hello
    world

When a variable is declared directly at the root of a source file, not inside any function, it exists in the global
scope. Unlike Python, the global scope is not a scope specific to each source file. *The global scope is shared amongst
all pieces of JavaScript code that are executed by an instance of the virtual machine*, which means on the same web
page.

::

    // file source1.js
    var x = "value1";

    // file source2.js
    var x = "value2";

If those two files are loaded by the same web page the variable ``x`` can only have one value, ``"value1"`` or
``"value2"``, depending on which file was loaded last. This is obviously a problem and it can be solved using the Module
Pattern (see later).

One last note about scopes is that any value which is assigned but not declared will be implicitly considered as being
part of the global scope. This means if you ever forget to use the ``var`` keyword the code will not crash but the
variable will be global to the application instead of local to the current function. This is a very common source of
errors in JavaScript.

::

    > function func1() {
          x = "hello";
      }
    > function func2() {
          console.log(x);
      }
    > func2();
    ReferenceError: x is not defined
    > func1();
    > func2();
    hello
    > x = "world";
    > func2();
    world

Arrays
------

The syntax of the arrays is pretty similar to Python:

::

    > var array = ["hello", "world"];
    > for (var i = 0; i < array.length; i++) {
          console.log(array[i]);
      }
    hello world

Please note the above syntax to iterate on arrays works but is ineffective, for real use-cases you should use
``_.each()`` or a similar function provided by a third-party library.

Like strings, the arrays have methods for different operations:

::

    > var array = [];
    > array.push("banana"); // adds an element at the end
    > array.push("tomato");
    > array;
    ["banana", "tomato"]
    > array.pop(); // removes the last element and returns it
    "tomato"
    > array;
    ["banana"]

Objects
-------

Object-oriented programming is possible in JavaScript, but it is very different compared to most other programming
language (except if you know Lua).

First of all, objects are dictionaries and dictionaries are objects. There is no difference in JavaScript. The syntax is
quite similar to Python's dictionaries but has alternative syntactic sugar depending if you prefer use a dictionary-like
or an object-like syntax. Demonstration:

::

    > var obj = {
          "key1": "hello", // dictionary-like declaration
          key2: "world", // object-like declaration
      };
    > console.log(obj["key1"]); // dictionary-like lookup
    hello
    > console.log(obj.key2); // object-like lookup
    world

``obj["key"]`` and ``obj.key`` have the exact same meaning. The first one will, by convention, be mostly used if you
want to make a lookup in a dictionary and the second one will be mostly used to access an object's attribute.

Methods can simply be defined by putting a function inside an object:

::

    > var person = {
          name: "John Smith",
          presentYourself: function() {
              return "Hello, my name is " + this.name;
          },
      };
    > person.presentYourself();
    "Hello, my name is John Smith"
    > person.name = "John Doe";
    > person.presentYourself();
    "Hello, my name is John Doe"

In JavaScript, each time a function is called it has and additional, implicitly declared variable called ``this``. When
a method is called on an object (using the usual ``object.method(arguments...)`` syntax), the ``this`` variable will
be a reference to the current object.

In the above example, we define a unique object containing all the attributes and methods necessary to make it work. But
that's not how most programming languages will handle object-oriented programming. They have a concept of class. A class
will contain the properties common to all its instances. There are no classes in JavaScript but it is possible to
reproduce that concept using *prototypes*.

::

    > var Person = function() { // this function will represent a class "Person"
          this.name = "JohnSmith";
      };
    > Person.prototype = {
          presentYourself: function() {
              return "Hello, my name is " + this.name;
          },
      };
    > var person = new Person();
    > person.presentYourself();
    "Hello, my name is John Smith"

Since prototype-based oriented object programming is a vast subject we will not cover it more in this guide, though you
can easily find some information about it on Internet.

Due to differences between prototype-based and class-based oriented object programming, and the habits of most
programmers, in OpenERP we chose to not use directly prototype-based oriented object programming. We use a high
level API that allows programmers to easily declare classes in a way that seems natural to people that are used to more
conventional programming languages. That subject will be covered later in this guide.