OpenERP Web Training
OpenERP Web Training ....................................................................................... 1
一、Introduction .......................................................................................... 3
Reminder about OpenERP Structure .................................................................... 3
About this Guide ................................................................................... 4
二、JavaScript Basics ...................................................................................... 5
Javascript 基础知识 ........................................................................................ 5
Preamble 前言 ....................................................................................... 5
What is a Web Application ? ................................................................. 5
A Note about JavaScript 关于 Javascript 的注记................................................ 6
Command Line Interpreter ........................................................................... 7
Basic Data Types 基本数据类型 ...................................................................... 8
Numbers 数字 ................................................................................ 8
Booleans 布尔值 ............................................................................. 9
Strings 字符串 .............................................................................. 9
Null 空值 .................................................................................. 10
Undefined 未定义 ........................................................................... 10
Implicit Type Conversions 类型转换 ................................................................. 10
Control Structures ................................................................................ 12
Functions 函数 .................................................................................... 13
Variables and Scopes 变量和作用范围 ................................................................ 14
Arrays 数组 ....................................................................................... 17
Objects ........................................................................................... 18
三、JavaScript Libraries JavaScrip 库 ..................................................................... 21
A First JavaScript Application 第一个 JavaScript 程序 ............................................... 21
Download and Launch the Start Application .................................................. 21
Architecture of the Application ............................................................ 22
The Module Pattern ......................................................................... 24
Debugging Tools 调试工具 ................................................................... 27
Underscore.js ..................................................................................... 28
HTML Manipulations with jQuery .................................................................... 31
The DOM .................................................................................... 31
jQuery Selectors jQuery 选择器 ............................................................. 33
jQuery Events JQuery 事件 .................................................................. 35
DOM Modifications with jQuery 用 JQuery 修改 DOM。 .......................................... 36
Exercise 练习 .............................................................................. 37
HTTP Requests with jQuery 使用 JQuery 的 HTTP 请求 .................................................. 38
The $.ajax() method $.ajax() 方法 ................................................... 40
Promises and Deferreds 承诺和递延 .......................................................... 42
Composing Deferreds 复合 Deferreds ........................................................ 45
Exercises 练习 ............................................................................ 50
四、OpenERP Web Framework ................................................................................. 52
1 / 141
A Simple Module to Test the Web Framework .......................................................... 53
OpenERP JavaScript Module ......................................................................... 57
Classes ........................................................................................... 59
Widgets Basics .................................................................................... 62
Your First Widget .......................................................................... 63
Display Content ............................................................................ 64
Widget Parents and Children ................................................................ 68
Destroying Widgets 销毁部件 ................................................................ 70
The QWeb Template Engine .......................................................................... 70
Using QWeb inside a Widget 在部件内使用 QWeb ................................................ 72
Widget Events and Properties 部件事件和属性 ........................................................ 82
Events 事件 ................................................................................ 82
Properties 属性 ............................................................................ 85
Exercise 练习 .............................................................................. 86
Widget Helpers 部件辅助工具 ....................................................................... 90
Widget 's jQuery Selector 部件的 jQeruy 选择器 .............................................. 90
Easier DOM Events Binding 简易 DOM 事件绑定 ................................................ 92
Development Guidelines 开发指南 ............................................................ 93
Modify Existent Widgets and Classes ................................................................ 95
Translations ...................................................................................... 97
Communication with the OpenERP Server .............................................................. 98
Contacting Models 与 Models 交互 ............................................................ 99
CompoundContext 复合上下文 ................................................................ 103
Queries 查询 .............................................................................. 105
Exercises 练习 ................................................................................... 107
五、OpenERP Web Components ............................................................................... 114
The Action Manager ............................................................................... 114
Using the Action Manager .................................................................. 115
使用 Action Manager ....................................................................... 115
Client Actions ................................................................................... 119
客户端 actions .................................................................................... 119
Architecture of the Views ........................................................................ 120
视图架构 ......................................................................................... 120
The View Manager .......................................................................... 120
视图管理器 ................................................................................ 121
The Views ................................................................................. 122
视图: .................................................................................... 122
The Form View Fields ............................................................................. 122
表单视图字段 ..................................................................................... 122
Creating a New Type of Field .............................................................. 127
创建一个新的字段类型 ...................................................................... 127
The Form View Custom Widgets ..................................................................... 135
表单视图自定义 Widgets ............................................................................ 135
2 / 141
一、Introduction
Reminder about OpenERP Structure
OpenERP 架构备忘
OpenERP contains the following parts:
OpenERP 包括下面的流程图部分。
The OpenERP server contains the server-side framework and handles
requests coming from clients.
OpenERP 服务包含服务端的框架,用来处理来自客户端的请求。
The PostgreSQL database contains our data.
PostgreSQL 数据库存储数据。
The modules implement the business logic.
Modules(模块)实现了业务逻辑。
The client web application communicates with the server and displays a
graphical user interface.
客户端应用程序与服务通信,并向用户显示图形界面。
This guide is all about web applications development and the OpenERP's web
client.
这个指南是关于 web 应用程序开发和 OpenERP 的 web 客户端。
3 / 141
About this Guide
This guide is a training material to teach OpenERP integrators how to create
web modules for OpenERP. The covered subjects are the following:
这个指南是一份培训资料,用来培训 OpenERP 的集成人员如何创建 web 模块,
覆盖的子项如下:
Javascript basics and good practices
Javascript 基础以及良好实践。
Basics of jQuery and Underscore (Javascript libraries used in
OpenERP's web client)
JQuery ,Underscore 基础(在 OpenERP 的 web 客户端内使用的
javascrip 库)
OpenERP's Javascript and web applications framework
OpenERP 的 javascript 和 web 应用程序框架。
Extension points for OpenERP's web client
OpenERP web 客户端的其他扩展点。
This guide assumes the reader followed the technical training about OpenERP
modules creation provided by OpenERP SA or have a good knowledge of
Python modules development. This guide also assume the reader is
experienced in object-oriented programming and has basic knowledge of at
least one programming language whose syntax is based on C (C++, Java,
C#,...). It may also refer to the Bazaar version control system. Finally, it is
necessary to have a minimal knowledge of HTML and CSS.
这个指南假设读者已经参与了 OpenERP SA 提供的模块创建技术培训,或
者在 Python 模块开发方面有良好的知识经验。也假设读者有面向对象编程经验,
至少有一门 C 系列语言(C++,java,C#)的编程经验。引用了 Bazaar 版本控
制系统。最后,需要有 HTML 和 CSS 的最基本的基础知识。
All provided examples assume you are developing under a Linux operating
system, most specifically Ubuntu or Debian. If you are using Windows, we
recommend you to get a virtual machine with Ubuntu. The
website http://virtualboxes.org/ provides pre-installed virtual machines images
for a lot of Linux distributions for free. They only require to install Virtual Box
which is also free.
提供的所有样例,是基于假设你具备在 Linux 下的开发经验,更具体的是在
Ubuntu 或 Debian 下的开发经验。如果你使用 Windows,我们建议你下载一个
4 / 141
Ubuntu 的虚拟机。站点 http://virtualboxes.org/ 免费提供许多安装好 Linux 各种
发行版的虚拟机镜像,使用这些镜像,需要安装免费虚拟机程序 Virtual Box。
二、JavaScript Basics
Javascript 基础知识
Preamble 前言
What is a Web Application ?
web 应用程序是什么?
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.
Web 应用程序是一个通过 Web 浏览器交付使用的简单应用程序,但近来,
这词有了更加特定的含义。
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.
以前 Web 应用程序的方式,包括 OpenERP6.0 和之前版本的工作机制,是
从服务端向用户端发送的描述应用程序 GUI 状态的整个 HTML 文档。这意味着
服务器为了响应客户端的每个请求交互,如按钮点击,搜索,导航历史记录,都
需要计算和重新发送一个新的 HTML 文档。
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,
5 / 141
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.
解决办法是用 JavaScript 创建一个完整的,独立的应用程序,该应用程序运
行在用户的 Web 浏览器上。这种类型的应用程序与类 PHP 网站相比,许多方
面更像传统桌面应用程序(例如:GTK, Swing, Qt, Windows Forms,...)。除了
编程语言和使用的库,与桌面应用程序唯一的区别是,Javascript 应用程序访问
OpenERP 网站,下载 Web 客户端,在用户浏览器上运行。
A Note about JavaScript 关于 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.
JavaScript 是运行在浏览器上的解释语言,而且是 Web 应用程序的一个事
实标准语言。如果你想开发 OpenERP 的 Web 客户端,就需要了解 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.
客观地说,JavaScript 不是一种优秀编程语言。1995 年,Netscape 出于商
业目的,用一个小团队在很短时间开发出 JavaScript。这并意味着 JavaScript
是历史上最广泛使用的编程语言。最初的设计就有很多问题,自从诞生以来,为
了向后兼容,就没有真正改善过。
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.
此外,由于 JavaScript 广受欢迎。Google 搜索 JavaScript 出来的文章,有
很多是没有受过正式计算机编程教育的人写的,即使一点不懂编程,也能够设法
通过复制粘贴代码得到一些效果。
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,
6 / 141
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.
然而,尽管有这有那的问题,JavaScript 语言的核心包含了优秀思想,让程
序员(喜欢基于原型编程和函数式编程)拥有很多的创造力。大多数 JavaScript
的缺点可以通过使用正确的模式和正确的工具避免。JavaScript 自身也有强大优
势。首先,JavaScript 非常快,启动快,运行快,部署快。JavaScript 使用 HTML
以及浏览器的多媒体 API 的能力,与桌面应用程序编程相比,能够创建非常漂
亮的应用程序,拥有良好的生产力。决定性的一点可能是,在这个星球上,
JavaScript 虚拟机事实上可在 99.99%的台式电脑上运行。
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.
最后,如果你是一个有着优秀 JavaScript 程序库的优秀程序员,则优势远远
超过了不便,为了普罗大众,努力使 JavaScript 和浏览器成为应用程序最好的
开发环境之一。
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 for
OpenERP's web module development. All this guide will assume you this
particular browser.
为了测试语言的基本特征,我们建议使用命令行解释器。大多数现代 Web
浏览器都提供 JavaScript 控制台,建议用谷歌浏览器协助 OpenERP 的 Web 模
块开发。本指南将假定你有这个浏览器。
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:
Chrome 安装好之后,任意打开一个网页,选择配置菜单的 工具>开发者工
具包,或使用快捷键 Ctrl + Shift+ I,开发工具包会显示在窗口底部。在工具包内
选择控制台面板。现在看起来像这样:
7 / 141
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:
请注意:JavaScript 没有整型。所有的数字都是浮点数。这与其他大多数编
程语言是个很大差异。这会影响些数学运算结果。例如:
> 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
8 / 141