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
|
odoo.define('web.ServicesMixin', function (require) {
"use strict";
var rpc = require('web.rpc');
/**
* @mixin
* @name ServicesMixin
*/
var ServicesMixin = {
/**
* @param {string} service
* @param {string} method
* @return {any} result of the service called
*/
call: function (service, method) {
var args = Array.prototype.slice.call(arguments, 2);
var result;
this.trigger_up('call_service', {
service: service,
method: method,
args: args,
callback: function (r) {
result = r;
},
});
return result;
},
/**
* @private
* @param {Object} libs - @see ajax.loadLibs
* @param {Object} [context] - @see ajax.loadLibs
* @param {Object} [tplRoute=this._loadLibsTplRoute] - @see ajax.loadLibs
* @returns {Promise}
*/
_loadLibs: function (libs, context, tplRoute) {
return this.call('ajax', 'loadLibs', libs, context, tplRoute || this._loadLibsTplRoute);
},
/**
* Builds and executes RPC query. Returns a promise resolved with
* the RPC result.
*
* @param {string} params either a route or a model
* @param {string} options if a model is given, this argument is a method
* @returns {Promise}
*/
_rpc: function (params, options) {
var query = rpc.buildQuery(params);
var prom = this.call('ajax', 'rpc', query.route, query.params, options, this);
if (!prom) {
prom = new Promise(function () {});
prom.abort = function () {};
}
var abort = prom.abort ? prom.abort : prom.reject;
if (!abort) {
throw new Error("a rpc promise should always have a reject function");
}
prom.abort = abort.bind(prom);
return prom;
},
loadFieldView: function (modelName, context, view_id, view_type, options) {
return this.loadViews(modelName, context, [[view_id, view_type]], options).then(function (result) {
return result[view_type];
});
},
loadViews: function (modelName, context, views, options) {
var self = this;
return new Promise(function (resolve) {
self.trigger_up('load_views', {
modelName: modelName,
context: context,
views: views,
options: options,
on_success: resolve,
});
});
},
loadFilters: function (modelName, actionId, context) {
var self = this;
return new Promise(function (resolve, reject) {
self.trigger_up('load_filters', {
modelName: modelName,
actionId: actionId,
context: context,
on_success: resolve,
});
});
},
createFilter: function (filter) {
var self = this;
return new Promise(function (resolve, reject) {
self.trigger_up('create_filter', {
filter: filter,
on_success: resolve,
});
});
},
deleteFilter: function (filterId) {
var self = this;
return new Promise(function (resolve, reject) {
self.trigger_up('delete_filter', {
filterId: filterId,
on_success: resolve,
});
});
},
// Session stuff
getSession: function () {
var session;
this.trigger_up('get_session', {
callback: function (result) {
session = result;
}
});
return session;
},
/**
* Informs the action manager to do an action. This supposes that the action
* manager can be found amongst the ancestors of the current widget.
* If that's not the case this method will simply return an unresolved
* promise.
*
* @param {any} action
* @param {any} options
* @returns {Promise}
*/
do_action: function (action, options) {
var self = this;
return new Promise(function (resolve, reject) {
self.trigger_up('do_action', {
action: action,
options: options,
on_success: resolve,
on_fail: reject,
});
});
},
/**
* Displays a notification.
*
* @param {Object} options
* @param {string} options.title
* @param {string} [options.subtitle]
* @param {string} [options.message]
* @param {string} [options.type='warning'] 'info', 'success', 'warning', 'danger' or ''
* @param {boolean} [options.sticky=false]
* @param {string} [options.className]
*/
displayNotification: function (options) {
return this.call('notification', 'notify', options);
},
/**
* @deprecated will be removed as soon as the notification system is reviewed
* @see displayNotification
*/
do_notify: function (title, message, sticky, className) {
return this.displayNotification({
type: 'warning',
title: title,
message: message,
sticky: sticky,
className: className,
});
},
/**
* @deprecated will be removed as soon as the notification system is reviewed
* @see displayNotification
*/
do_warn: function (title, message, sticky, className) {
console.warn(title, message);
return this.displayNotification({
type: 'danger',
title: title,
message: message,
sticky: sticky,
className: className,return
});
},
};
return ServicesMixin;
});
|