summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/_deprecated/data.js
diff options
context:
space:
mode:
authorRobot Odoo <xmo+roboto@odoo.com>2019-03-06 21:18:36 +0100
committerGitHub <noreply@github.com>2019-03-06 21:18:36 +0100
commitfc60c49ba3cb30fc4e436c9ffc1661d707fe858c (patch)
tree110bf47078ce4862cfad05aa061ba1238ebc99e6 /addons/web/static/src/js/_deprecated/data.js
parent414ac1a49a61e7f68b4c8dab8688a5b8840aefed (diff)
parent53e7050ce0c1bd873f239bce8ad81ba42c19d57a (diff)
[MERGE] web,*: update code base to jQuery 3.*
The goal of this task is to update jQuery from version 1.11 to 3.x. Task-id : 1896658 As we are skipping 2 magor versions, there are some breaking changes in the jquery library described here: https://jquery.com/upgrade-guide/3.0/ Main changes are: ------------------------- 1. Update the libraries jQuery, jQueryUI, QUnit and select2 to their latest version. The link to generate the build of jQueryUI with the used components: [jquery UI build](https://jqueryui.com/download/#!version=1.12.1&components=111110010101111110100010110000011010000000000000) 2. Use native promises ([a complete explanation about promises here](https://github.com/getify/You-Dont-Know-JS/blob/master/async%20%26%20performance/ch3.md)) everywhere possible: In order to [be A+ compliant](https://promisesaplus.com/), the jq3 deferred uses `setTimeout(..., 0)` for every then and catch, which takes between 4 and 10 ms by call. This would be way too slow to use in Odoo. To prevent this, we have used the native Promises everywhere possible. Be it with jq deferred or native promises, all promises are now asynchronous, this example will ALWAYS result in printing first "just after blabla" then "blabla" because the `then` function will always be asynchronous (opposite to what jquery used to do) ``` function () { Promise.resolve("blabla").then(console.log); console.log("just after blabla"); } // console: // just after blabla // blabla ``` 3. On native promises, you should see the catch like a try/catch in javascript, meaning that it will catch all errors including coding errors: For this we have introduced the concept of `guardedCatch`, which will be executed on promise rejection unless the promise is rejected because there is a code error or runtime error (calls on undefined, etc). 4. Native Promises do not have `done`, `fail` and `always` methods nor does it have the `state` method - `fail` can be replaced with `guardedCatch` - `return $.when(x).done(function() ...)` can be replaced with ``` var x = Promise.resolve(); // we get the x promise in a local variable, without then x.then(function() ...); // we add a new handler to x. This operation returns a new promise we don't care about return x; // we return the x promise that will be completed without waiting for the then ``` - `$.when().done(function ...)` without the return can be replaced by `then(function ....)`, because if it is not returned, it doesn't matter what is chained or not `$.when().always(function() ...)` can be replaced with ``` var always = function() ... Promise.resolve().then(always).guardedCatch(always); ``` - It is not possible to inspect the state of a promise, except in debug tools of chrome and firefox. Any code that relies on `$.Deferred.state()` should be rewritten to use flags in `then` and `catch` handlers. Remember that it is not possible to execute the handlers in a synchroneous manner, so when a promise is created, its `then` will never be called on the same execution stack. At best it will be called on the next microtask tick (before the next setTimeout). 5. We have introduced the concept of guardedCatch, to copy the behavior of the old fail: The guarded catch will only execute the handler in parameter if the rejection reason is not an `Error` ``` var failingPromise = Promise.reject({message:"I am the rejection reason"}); failingPromise.guardedCatch(function(reason) {console.log(reason);}); // is called, then returns a rejected promise failingPromise.catch(function(reason) {console.log(reason);}); // is called ``` will write the reason "I am the rejection reason" to the console twice: once because the `guardedCatch` will execute (and return a rejected `Promise` with the same reason it was called), a second time because the `catch` will catch the rejected reason returned by the `guardedCatch` ``` var promiseWithError = Promise.resolve().then(function() { a.b = 0; // <-- the variable a does not exist, so a.b is an error }); promiseWithError.guardedCatch(function(reason) {console.log(reason);}); // will not be called promiseWithError.catch(function(reason) {console.log(reason);}); // will be called ``` contrary to how deferred used to work, here the catch will be called because it behaves like a `try... catch...` and will catch the coding error like if it was a business error. The `guardedCatch` however does not call the handler if the rejection reason is an error. In all Odoo code, we never reject a promise with an error and always use objects to defined the rejected reason, so it is a good idea to use `guardedCatch` everywhere. 6. Native Promises can have only one parameter passed to a `then` function or a `catch` function: `xxx.then(function(param1, param2) {...}); // param2 will never have a value` - we can replace `$.when(prom1, prom2).then(function(result1, result2) { ... });` by ``` Promise.all([prom1, prom2]).then(function(results) { var result1 = results[1]; var result2 = results[2]; ... }); ``` 7. We are now using ES7 code in the unit tests (async await). closes odoo/odoo#28457 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com> Co-authored-by: Aaron Bohy <aab@odoo.com> Co-authored-by: Christophe Matthieu <chm@odoo.com> Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com> Co-authored-by: David Monjoie <dmo@odoo.com> Co-authored-by: Martin Geubelle <mge@odoo.com> Co-authored-by: svs-odoo <svs@odoo.com> Co-authored-by: Vincent Schippefilt <vsc@odoo.com>
Diffstat (limited to 'addons/web/static/src/js/_deprecated/data.js')
-rw-r--r--addons/web/static/src/js/_deprecated/data.js75
1 files changed, 44 insertions, 31 deletions
diff --git a/addons/web/static/src/js/_deprecated/data.js b/addons/web/static/src/js/_deprecated/data.js
index 984f0bbaddb..acac959fcc8 100644
--- a/addons/web/static/src/js/_deprecated/data.js
+++ b/addons/web/static/src/js/_deprecated/data.js
@@ -101,7 +101,7 @@ var Query = Class.extend({
* Fetches the first record matching the query, or null
*
* @param {Object} [options] additional options for the rpc() method
- * @returns {jQuery.Deferred<Object|null>}
+ * @returns {Promise<Object|null>}
*/
first: function (options) {
var self = this;
@@ -115,7 +115,7 @@ var Query = Class.extend({
* Fetches all records matching the query
*
* @param {Object} [options] additional options for the rpc() method
- * @returns {jQuery.Deferred<Array<>>}
+ * @returns {Promise<Array<>>}
*/
all: function (options) {
return this._execute(options);
@@ -123,10 +123,10 @@ var Query = Class.extend({
/**
* Fetches the number of records matching the query in the database
*
- * @returns {jQuery.Deferred<Number>}
+ * @returns {Promise<Number>}
*/
count: function () {
- if (this._count !== undefined) { return $.when(this._count); }
+ if (this._count !== undefined) { return Promise.resolve(this._count); }
return this._model.call(
'search_count', [this._filter], {
context: this._model.context(this._context)});
@@ -367,11 +367,12 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* @param {Array} ids identifiers of the records to read
* @param {Array} [fields] fields to read and return, by default all fields are returned
* @param {Object} [options]
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
read_ids: function (ids, fields, options) {
- if (_.isEmpty(ids))
- return $.Deferred().resolve([]);
+ if (_.isEmpty(ids)) {
+ return Promise.resolve([]);
+ }
options = options || {};
var method = 'read';
@@ -405,7 +406,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* @params {Object} [options]
* @param {Number} [options.offset=0] The index from which selected records should be returned
* @param {Number} [options.limit=null] The maximum number of records to return
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
read_slice: function (fields, options) {
var self = this;
@@ -414,21 +415,23 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
.limit(options.limit || false)
.offset(options.offset || 0)
.all();
- return this.orderer.add(query).done(function (records) {
+ var prom = this.orderer.add(query);
+ prom.then(function (records) {
self.ids = _(records).pluck('id');
});
+ return prom;
},
/**
* Reads the current dataset record (from its index)
*
* @params {Array} [fields] fields to read and return, by default all fields are returned
* @param {Object} [options.context] context data to add to the request payload, on top of the DataSet's own context
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
read_index: function (fields, options) {
options = options || {};
return this.read_ids([this.ids[this.index]], fields, options).then(function (records) {
- if (_.isEmpty(records)) { return $.Deferred().reject().promise(); }
+ if (_.isEmpty(records)) { return Promise.reject(); }
return records[0];
});
},
@@ -437,7 +440,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
*
* @param {Array} [fields] fields to get default values for, by default all defaults are read
* @param {Object} [options.context] context data to add to the request payload, on top of the DataSet's own context
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
default_get: function (fields, options) {
options = options || {};
@@ -451,15 +454,17 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* @param {Object} options Dictionary that can contain the following keys:
* - readonly_fields: Values from readonly fields that were updated by
* on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
create: function (data, options) {
var self = this;
- return this._model.call('create', [data], {
+ var prom = this._model.call('create', [data], {
context: this.get_context()
- }).done(function () {
+ });
+ prom.then(function () {
self.trigger('dataset_changed', data, options);
});
+ return prom;
},
/**
* Saves the provided data in an existing db record
@@ -470,16 +475,18 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* - context: The context to use in the server-side call.
* - readonly_fields: Values from readonly fields that were updated by
* on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
write: function (id, data, options) {
options = options || {};
var self = this;
- return this._model.call('write', [[id], data], {
+ var prom = this._model.call('write', [[id], data], {
context: this.get_context(options.context)
- }).done(function () {
+ });
+ prom.then(function () {
self.trigger('dataset_changed', id, data, options);
});
+ return prom;
},
/**
* Deletes an existing record from the database
@@ -488,11 +495,13 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
*/
unlink: function (ids) {
var self = this;
- return this._model.call('unlink', [ids], {
+ var prom = this._model.call('unlink', [ids], {
context: this.get_context()
- }).done(function () {
+ });
+ prom.then(function () {
self.trigger('dataset_changed', ids);
});
+ return prom;
},
/**
* Calls an arbitrary RPC method
@@ -501,7 +510,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* @param {Array} [args] arguments to pass to the method
* @param {Function} callback
* @param {Function} error_callback
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
call: function (method, args) {
return this._model.call(method, args);
@@ -511,7 +520,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
*
* @param {String} method
* @param {Array} [args]
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
call_button: function (method, args) {
return this._model.call_button(method, args);
@@ -520,7 +529,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* Fetches the "readable name" for records, based on intrinsic rules
*
* @param {Array} ids
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
name_get: function (ids) {
return this._model.call('name_get', [ids], {context: this.get_context()});
@@ -532,7 +541,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* @param {String} [operator='ilike'] matching operator to use with the provided name value
* @param {Number} [limit=0] maximum number of matches to return
* @param {Function} callback function to call with name_search result
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
name_search: function (name, domain, operator, limit) {
return this._model.call('name_search', {
@@ -603,7 +612,7 @@ var DataSet = Class.extend(mixins.PropertiesMixin, {
* Resequence records.
*
* @param {Array} ids identifiers of the records to resequence
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
resequence: function (ids, options) {
options = options || {};
@@ -643,7 +652,7 @@ var DataSetStatic = DataSet.extend({
unlink: function (ids) {
this.set_ids(_.without.apply(null, [this.ids].concat(ids)));
this.trigger('unlink', ids);
- return $.Deferred().resolve({result: true});
+ return Promise.resolve({result: true});
},
});
@@ -674,7 +683,7 @@ var DataSetSearch = DataSet.extend({
* @param {Array} [options.domain] domain data to add to the request payload, ANDed with the dataset's domain
* @param {Number} [options.offset=0] The index from which selected records should be returned
* @param {Number} [options.limit=null] The maximum number of records to return
- * @returns {$.Deferred}
+ * @returns {Promise}
*/
read_slice: function (fields, options) {
options = options || {};
@@ -686,11 +695,13 @@ var DataSetSearch = DataSet.extend({
.limit(options.limit || false);
q = q.order_by.apply(q, this._sort);
- return this.orderer.add(q.all()).done(function (records) {
+ var prom = this.orderer.add(q.all());
+ prom.then(function (records) {
// FIXME: not sure about that one, *could* have discarded count
- q.count().done(function (count) { self._length = count; });
+ q.count().then(function (count) { self._length = count; });
self.ids = _(records).pluck('id');
});
+ return prom;
},
get_domain: function (other_domain) {
return this._model.domain(other_domain);
@@ -717,10 +728,12 @@ var DataSetSearch = DataSet.extend({
},
unlink: function (ids, callback, error_callback) {
var self = this;
- return this._super(ids).done(function () {
+ var prom = this._super(ids);
+ prom.then(function () {
self.remove_ids( ids);
self.trigger("dataset_changed", ids, callback, error_callback);
});
+ return prom;
},
size: function () {
if (this._length !== null) {
@@ -801,7 +814,7 @@ var Model = Class.extend({
* @param {Array} [args] positipyEvalonal arguments
* @param {Object} [kwargs] keyword arguments
* @param {Object} [options] additional options for the rpc() method
- * @returns {jQuery.Deferred<>} call result
+ * @returns {Promise<>} call result
*/
call: function (method, args, kwargs, options) {
args = args || [];