2 openerp.web.data = function(instance) {
5 * Serializes the sort criterion array of a dataset into a form which can be
6 * consumed by OpenERP's RPC APIs.
8 * @param {Array} criterion array of fields, from first to last criteria, prefixed with '-' for reverse sorting
9 * @returns {String} SQL-like sorting string (``ORDER BY``) clause
11 instance.web.serialize_sort = function (criterion) {
12 return _.map(criterion,
14 if (criteria[0] === '-') {
15 return criteria.slice(1) + ' DESC';
17 return criteria + ' ASC';
21 instance.web.Query = instance.web.Class.extend({
22 init: function (model, fields) {
24 this._fields = fields;
31 clone: function (to_set) {
32 to_set = to_set || {};
33 var q = new instance.web.Query(this._model, this._fields);
34 q._context = this._context;
35 q._filter = this._filter;
36 q._limit = this._limit;
37 q._offset = this._offset;
38 q._order_by = this._order_by;
40 for(var key in to_set) {
41 if (!to_set.hasOwnProperty(key)) { continue; }
44 q._filter = new instance.web.CompoundDomain(
45 q._filter, to_set.filter);
48 q._context = new instance.web.CompoundContext(
49 q._context, to_set.context);
54 q['_' + key] = to_set[key];
59 _execute: function () {
61 return instance.session.rpc('/web/dataset/search_read', {
62 model: this._model.name,
63 fields: this._fields || false,
64 domain: instance.web.pyeval.eval('domains',
65 [this._model.domain(this._filter)]),
66 context: instance.web.pyeval.eval('contexts',
67 [this._model.context(this._context)]),
70 sort: instance.web.serialize_sort(this._order_by)
71 }).then(function (results) {
72 self._count = results.length;
73 return results.records;
77 * Fetches the first record matching the query, or null
79 * @returns {jQuery.Deferred<Object|null>}
83 return this.clone({limit: 1})._execute().then(function (records) {
85 if (records.length) { return records[0]; }
90 * Fetches all records matching the query
92 * @returns {jQuery.Deferred<Array<>>}
95 return this._execute();
98 * Fetches the number of records matching the query in the database
100 * @returns {jQuery.Deferred<Number>}
103 if (this._count != undefined) { return $.when(this._count); }
104 return this._model.call(
105 'search_count', [this._filter], {
106 context: this._model.context(this._context)});
109 * Performs a groups read according to the provided grouping criterion
111 * @param {String|Array<String>} grouping
112 * @returns {jQuery.Deferred<Array<openerp.web.QueryGroup>> | null}
114 group_by: function (grouping) {
115 if (grouping === undefined) {
119 if (!(grouping instanceof Array)) {
120 grouping = _.toArray(arguments);
122 if (_.isEmpty(grouping)) { return null; }
126 var ctx = instance.web.pyeval.eval(
127 'context', this._model.context(this._context));
128 return this._model.call('read_group', {
130 fields: _.uniq(grouping.concat(this._fields || [])),
131 domain: this._model.domain(this._filter),
132 context: this._model.context(this._context),
133 offset: this._offset,
135 orderby: instance.web.serialize_sort(this._order_by) || false
136 }).then(function (results) {
137 return _(results).map(function (result) {
138 // FIX: querygroup initialization
139 result.__context = result.__context || {};
140 result.__context.group_by = result.__context.group_by || [];
141 _.defaults(result.__context, ctx);
142 return new instance.web.QueryGroup(
143 self._model.name, grouping[0], result);
148 * Creates a new query with the union of the current query's context and
151 * @param context context data to add to the query
152 * @returns {openerp.web.Query}
154 context: function (context) {
155 if (!context) { return this; }
156 return this.clone({context: context});
159 * Creates a new query with the union of the current query's filter and
162 * @param domain domain data to AND with the current query filter
163 * @returns {openerp.web.Query}
165 filter: function (domain) {
166 if (!domain) { return this; }
167 return this.clone({filter: domain});
170 * Creates a new query with the provided limit replacing the current
173 * @param {Number} limit maximum number of records the query should retrieve
174 * @returns {openerp.web.Query}
176 limit: function (limit) {
177 return this.clone({limit: limit});
180 * Creates a new query with the provided offset replacing the current
183 * @param {Number} offset number of records the query should skip before starting its retrieval
184 * @returns {openerp.web.Query}
186 offset: function (offset) {
187 return this.clone({offset: offset});
190 * Creates a new query with the provided ordering parameters replacing
191 * those of the current query
193 * @param {String...} fields ordering clauses
194 * @returns {openerp.web.Query}
196 order_by: function (fields) {
197 if (fields === undefined) { return this; }
198 if (!(fields instanceof Array)) {
199 fields = _.toArray(arguments);
201 if (_.isEmpty(fields)) { return this; }
202 return this.clone({order_by: fields});
206 instance.web.QueryGroup = instance.web.Class.extend({
207 init: function (model, grouping_field, read_group_group) {
208 // In cases where group_by_no_leaf and no group_by, the result of
209 // read_group has aggregate fields but no __context or __domain.
210 // Create default (empty) values for those so that things don't break
211 var fixed_group = _.extend(
212 {__context: {group_by: []}, __domain: []},
216 _(fixed_group).each(function (value, key) {
217 if (key.indexOf('__') === 0
218 || key === grouping_field
219 || key === grouping_field + '_count') {
222 aggregates[key] = value || 0;
225 this.model = new instance.web.Model(
226 model, fixed_group.__context, fixed_group.__domain);
228 var group_size = fixed_group[grouping_field + '_count'] || fixed_group.__count || 0;
229 var leaf_group = fixed_group.__context.group_by.length === 0;
231 folded: !!(fixed_group.__fold),
232 grouped_on: grouping_field,
233 // if terminal group (or no group) and group_by_no_leaf => use group.__count
235 value: fixed_group[grouping_field],
236 // A group is open-able if it's not a leaf in group_by_no_leaf mode
237 has_children: !(leaf_group && fixed_group.__context['group_by_no_leaf']),
239 aggregates: aggregates
242 get: function (key) {
243 return this.attributes[key];
245 subgroups: function () {
246 return this.model.query().group_by(this.model.context().group_by);
249 return this.model.query.apply(this.model, arguments);
253 instance.web.Model = instance.web.Class.extend({
255 * @constructs instance.web.Model
256 * @extends instance.web.Class
258 * @param {String} model_name name of the OpenERP model this object is bound to
259 * @param {Object} [context]
260 * @param {Array} [domain]
262 init: function (model_name, context, domain) {
263 this.name = model_name;
264 this._context = context || {};
265 this._domain = domain || [];
268 * @deprecated does not allow to specify kwargs, directly use call() instead
270 get_func: function (method_name) {
273 return self.call(method_name, _.toArray(arguments));
277 * Call a method (over RPC) on the bound OpenERP model.
279 * @param {String} method name of the method to call
280 * @param {Array} [args] positional arguments
281 * @param {Object} [kwargs] keyword arguments
282 * @param {Object} [options] additional options for the rpc() method
283 * @returns {jQuery.Deferred<>} call result
285 call: function (method, args, kwargs, options) {
287 kwargs = kwargs || {};
288 if (!_.isArray(args)) {
289 // call(method, kwargs)
293 instance.web.pyeval.ensure_evaluated(args, kwargs);
294 var debug = instance.session.debug ? '/'+this.name+':'+method : '';
295 return instance.session.rpc('/web/dataset/call_kw' + debug, {
303 * Fetches a Query instance bound to this model, for searching
305 * @param {Array<String>} [fields] fields to ultimately fetch during the search
306 * @returns {instance.web.Query}
308 query: function (fields) {
309 return new instance.web.Query(this, fields);
312 * Executes a signal on the designated workflow, on the bound OpenERP model
314 * @param {Number} id workflow identifier
315 * @param {String} signal signal to trigger on the workflow
317 exec_workflow: function (id, signal) {
318 return instance.session.rpc('/web/dataset/exec_workflow', {
325 * Fetches the model's domain, combined with the provided domain if any
327 * @param {Array} [domain] to combine with the model's internal domain
328 * @returns The model's internal domain, or the AND-ed union of the model's internal domain and the provided domain
330 domain: function (domain) {
331 if (!domain) { return this._domain; }
332 return new instance.web.CompoundDomain(
333 this._domain, domain);
336 * Fetches the combination of the user's context and the domain context,
337 * combined with the provided context if any
339 * @param {Object} [context] to combine with the model's internal context
340 * @returns The union of the user's context and the model's internal context, as well as the provided context if any. In that order.
342 context: function (context) {
343 return new instance.web.CompoundContext(
344 instance.session.user_context, this._context, context || {});
347 * Button action caller, needs to perform cleanup if an action is returned
348 * from the button (parsing of context and domain, and fixup of the views
349 * collection for act_window actions)
351 * FIXME: remove when evaluator integrated
353 call_button: function (method, args) {
354 instance.web.pyeval.ensure_evaluated(args, {});
355 return instance.session.rpc('/web/dataset/call_button', {
358 // Should not be necessary anymore. Integrate remote in this?
360 context_id: args.length - 1,
366 instance.web.DataSet = instance.web.Class.extend(instance.web.PropertiesMixin, {
368 * Collection of OpenERP records, used to share records and the current selection between views.
370 * @constructs instance.web.DataSet
372 * @param {String} model the OpenERP model this dataset will manage
374 init: function(parent, model, context) {
375 instance.web.PropertiesMixin.init.call(this);
377 this.context = context || {};
380 this._model = new instance.web.Model(model, context);
382 previous: function () {
384 if (!this.ids.length) {
386 } else if (this.index < 0) {
387 this.index = this.ids.length - 1;
393 if (!this.ids.length) {
395 } else if (this.index >= this.ids.length) {
400 select_id: function(id) {
401 var idx = this.get_id_index(id);
409 get_id_index: function(id) {
410 for (var i=0, ii=this.ids.length; i<ii; i++) {
411 // Here we use type coercion because of the mess potentially caused by
412 // OpenERP ids fetched from the DOM as string. (eg: dhtmlxcalendar)
413 // OpenERP ids can be non-numeric too ! (eg: recursive events in calendar)
414 if (id == this.ids[i]) {
423 * @param {Array} ids identifiers of the records to read
424 * @param {Array} fields fields to read and return, by default all fields are returned
425 * @returns {$.Deferred}
427 read_ids: function (ids, fields, options) {
428 options = options || {};
429 // TODO: reorder results to match ids list
430 return this._model.call('read',
431 [ids, fields || false],
432 {context: this._model.context(options.context)});
435 * Read a slice of the records represented by this DataSet, based on its
436 * domain and context.
438 * @param {Array} [fields] fields to read and return, by default all fields are returned
439 * @params {Object} [options]
440 * @param {Number} [options.offset=0] The index from which selected records should be returned
441 * @param {Number} [options.limit=null] The maximum number of records to return
442 * @returns {$.Deferred}
444 read_slice: function (fields, options) {
446 options = options || {};
447 return this._model.query(fields)
448 .limit(options.limit || false)
449 .offset(options.offset || 0)
450 .all().done(function (records) {
451 self.ids = _(records).pluck('id');
455 * Reads the current dataset record (from its index)
457 * @params {Array} [fields] fields to read and return, by default all fields are returned
458 * @param {Object} [options.context] context data to add to the request payload, on top of the DataSet's own context
459 * @returns {$.Deferred}
461 read_index: function (fields, options) {
462 options = options || {};
463 return this.read_ids([this.ids[this.index]], fields, options).then(function (records) {
464 if (_.isEmpty(records)) { return $.Deferred().reject().promise(); }
469 * Reads default values for the current model
471 * @param {Array} [fields] fields to get default values for, by default all defaults are read
472 * @param {Object} [options.context] context data to add to the request payload, on top of the DataSet's own context
473 * @returns {$.Deferred}
475 default_get: function(fields, options) {
476 options = options || {};
477 return this._model.call('default_get',
478 [fields], {context: this.get_context(options.context)});
481 * Creates a new record in db
483 * @param {Object} data field values to set on the new record
484 * @param {Object} options Dictionary that can contain the following keys:
485 * - readonly_fields: Values from readonly fields that were updated by
486 * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
487 * @returns {$.Deferred}
489 create: function(data, options) {
490 return this._model.call('create', [data], {context: this.get_context()});
493 * Saves the provided data in an existing db record
495 * @param {Number|String} id identifier for the record to alter
496 * @param {Object} data field values to write into the record
497 * @param {Object} options Dictionary that can contain the following keys:
498 * - context: The context to use in the server-side call.
499 * - readonly_fields: Values from readonly fields that were updated by
500 * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
501 * @returns {$.Deferred}
503 write: function (id, data, options) {
504 options = options || {};
505 return this._model.call('write', [[id], data], {context: this.get_context(options.context)}).done(this.trigger('dataset_changed', id, data, options));
508 * Deletes an existing record from the database
510 * @param {Number|String} ids identifier of the record to delete
512 unlink: function(ids) {
513 return this._model.call('unlink', [ids], {context: this.get_context()}).done(this.trigger('dataset_changed', ids));
516 * Calls an arbitrary RPC method
518 * @param {String} method name of the method (on the current model) to call
519 * @param {Array} [args] arguments to pass to the method
520 * @param {Function} callback
521 * @param {Function} error_callback
522 * @returns {$.Deferred}
524 call: function (method, args) {
525 return this._model.call(method, args);
528 * Calls a button method, usually returning some sort of action
530 * @param {String} method
531 * @param {Array} [args]
532 * @returns {$.Deferred}
534 call_button: function (method, args) {
535 return this._model.call_button(method, args);
538 * Fetches the "readable name" for records, based on intrinsic rules
541 * @returns {$.Deferred}
543 name_get: function(ids) {
544 return this._model.call('name_get', [ids], {context: this.get_context()});
548 * @param {String} name name to perform a search for/on
549 * @param {Array} [domain=[]] filters for the objects returned, OpenERP domain
550 * @param {String} [operator='ilike'] matching operator to use with the provided name value
551 * @param {Number} [limit=0] maximum number of matches to return
552 * @param {Function} callback function to call with name_search result
553 * @returns {$.Deferred}
555 name_search: function (name, domain, operator, limit) {
556 return this._model.call('name_search', {
558 args: domain || false,
559 operator: operator || 'ilike',
560 context: this._model.context(),
567 name_create: function(name) {
568 return this._model.call('name_create', [name], {context: this.get_context()});
570 exec_workflow: function (id, signal) {
571 return this._model.exec_workflow(id, signal);
573 get_context: function(request_context) {
574 return this._model.context(request_context);
577 * Reads or changes sort criteria on the dataset.
579 * If not provided with any argument, serializes the sort criteria to
580 * an SQL-like form usable by OpenERP's ORM.
582 * If given a field, will set that field as first sorting criteria or,
583 * if the field is already the first sorting criteria, will reverse it.
585 * @param {String} [field] field to sort on, reverses it (toggle from ASC to DESC) if already the main sort criteria
586 * @param {Boolean} [force_reverse=false] forces inserting the field as DESC
587 * @returns {String|undefined}
589 sort: function (field, force_reverse) {
591 return instance.web.serialize_sort(this._sort);
593 var reverse = force_reverse || (this._sort[0] === field);
594 this._sort.splice.apply(
595 this._sort, [0, this._sort.length].concat(
596 _.without(this._sort, field, '-' + field)));
598 this._sort.unshift((reverse ? '-' : '') + field);
602 return this.ids.length;
604 alter_ids: function(n_ids) {
608 * Resequence records.
610 * @param {Array} ids identifiers of the records to resequence
611 * @returns {$.Deferred}
613 resequence: function (ids, options) {
614 options = options || {};
615 return instance.session.rpc('/web/dataset/resequence', {
618 context: instance.web.pyeval.eval(
619 'context', this.get_context(options.context)),
620 }).then(function (results) {
626 instance.web.DataSetStatic = instance.web.DataSet.extend({
627 init: function(parent, model, context, ids) {
629 this._super(parent, model, context);
631 this.ids = ids || [];
633 read_slice: function (fields, options) {
634 options = options || {};
635 fields = fields || {};
636 var offset = options.offset || 0,
637 limit = options.limit || false;
638 var end_pos = limit && limit !== -1 ? offset + limit : this.ids.length;
639 return this.read_ids(this.ids.slice(offset, end_pos), fields);
641 set_ids: function (ids) {
643 if (ids.length === 0) {
645 } else if (this.index >= ids.length - 1) {
646 this.index = ids.length - 1;
649 unlink: function(ids) {
650 this.set_ids(_.without.apply(null, [this.ids].concat(ids)));
651 this.trigger('unlink', ids);
652 return $.Deferred().resolve({result: true});
656 instance.web.DataSetSearch = instance.web.DataSet.extend({
658 * @constructs instance.web.DataSetSearch
659 * @extends instance.web.DataSet
661 * @param {Object} parent
662 * @param {String} model
663 * @param {Object} context
664 * @param {Array} domain
666 init: function(parent, model, context, domain) {
667 this._super(parent, model, context);
668 this.domain = domain || [];
671 this._model = new instance.web.Model(model, context, domain);
674 * Read a slice of the records represented by this DataSet, based on its
675 * domain and context.
677 * @params {Object} options
678 * @param {Array} [options.fields] fields to read and return, by default all fields are returned
679 * @param {Object} [options.context] context data to add to the request payload, on top of the DataSet's own context
680 * @param {Array} [options.domain] domain data to add to the request payload, ANDed with the dataset's domain
681 * @param {Number} [options.offset=0] The index from which selected records should be returned
682 * @param {Number} [options.limit=null] The maximum number of records to return
683 * @returns {$.Deferred}
685 read_slice: function (fields, options) {
686 options = options || {};
688 var q = this._model.query(fields || false)
689 .filter(options.domain)
690 .context(options.context)
691 .offset(options.offset || 0)
692 .limit(options.limit || false);
693 q = q.order_by.apply(q, this._sort);
695 return q.all().done(function (records) {
696 // FIXME: not sure about that one, *could* have discarded count
697 q.count().done(function (count) { self._length = count; });
698 self.ids = _(records).pluck('id');
701 get_domain: function (other_domain) {
702 this._model.domain(other_domain);
704 unlink: function(ids, callback, error_callback) {
706 return this._super(ids).done(function(result) {
707 self.ids = _(self.ids).difference(ids);
711 if (self.index !== null) {
712 self.index = self.index <= self.ids.length - 1 ?
713 self.index : (self.ids.length > 0 ? self.ids.length -1 : 0);
715 self.trigger("dataset_changed", ids, callback, error_callback);
719 if (this._length != null) {
722 return this._super();
726 instance.web.BufferedDataSet = instance.web.DataSetStatic.extend({
727 virtual_id_prefix: "one2many_v_id_",
730 this._super.apply(this, arguments);
732 this.last_default_get = {};
734 default_get: function(fields, options) {
736 return this._super(fields, options).done(function(res) {
737 self.last_default_get = res;
740 create: function(data, options) {
742 id:_.uniqueId(this.virtual_id_prefix),
743 values: _.extend({}, data, (options || {}).readonly_fields || {}),
744 defaults: this.last_default_get
746 this.to_create.push(_.extend(_.clone(cached), {values: _.clone(data)}));
747 this.cache.push(cached);
748 return $.Deferred().resolve(cached.id).promise();
750 write: function (id, data, options) {
752 var record = _.detect(this.to_create, function(x) {return x.id === id;});
753 record = record || _.detect(this.to_write, function(x) {return x.id === id;});
756 for (var k in data) {
757 if (record.values[k] === undefined || record.values[k] !== data[k]) {
762 $.extend(record.values, data);
765 record = {id: id, values: data};
766 self.to_write.push(record);
768 var cached = _.detect(this.cache, function(x) {return x.id === id;});
770 cached = {id: id, values: {}};
771 this.cache.push(cached);
773 $.extend(cached.values, _.extend({}, record.values, (options || {}).readonly_fields || {}));
775 this.trigger("dataset_changed", id, data, options);
776 return $.Deferred().resolve(true).promise();
778 unlink: function(ids, callback, error_callback) {
780 _.each(ids, function(id) {
781 if (! _.detect(self.to_create, function(x) { return x.id === id; })) {
782 self.to_delete.push({id: id})
785 this.to_create = _.reject(this.to_create, function(x) { return _.include(ids, x.id);});
786 this.to_write = _.reject(this.to_write, function(x) { return _.include(ids, x.id);});
787 this.cache = _.reject(this.cache, function(x) { return _.include(ids, x.id);});
788 this.set_ids(_.without.apply(_, [this.ids].concat(ids)));
789 this.trigger("dataset_changed", ids, callback, error_callback);
790 return $.async_when({result: true}).done(callback);
792 reset_ids: function(ids) {
798 this.delete_all = false;
800 read_ids: function (ids, fields, options) {
803 _.each(ids, function(id) {
804 var cached = _.detect(self.cache, function(x) {return x.id === id;});
805 var created = _.detect(self.to_create, function(x) {return x.id === id;});
807 _.each(fields, function(x) {if (cached.values[x] === undefined)
808 cached.values[x] = created.defaults[x] || false;});
810 if (!cached || !_.all(fields, function(x) {return cached.values[x] !== undefined}))
814 var completion = $.Deferred();
815 var return_records = function() {
816 var records = _.map(ids, function(id) {
817 return _.extend({}, _.detect(self.cache, function(c) {return c.id === id;}).values, {"id": id});
819 if (self.debug_mode) {
820 if (_.include(records, undefined)) {
821 throw "Record not correctly loaded";
824 var sort_fields = self._sort,
825 compare = function (v1, v2) {
826 return (v1 < v2) ? -1
830 // Array.sort is not necessarily stable. We must be careful with this because
831 // sorting an array where all items are considered equal is a worst-case that
832 // will randomize the array with an unstable sort! Therefore we must avoid
833 // sorting if there are no sort_fields (i.e. all items are considered equal)
834 // See also: http://ecma262-5.com/ELS5_Section_15.htm#Section_15.4.4.11
835 // http://code.google.com/p/v8/issues/detail?id=90
836 if (sort_fields.length) {
837 records.sort(function (a, b) {
838 return _.reduce(sort_fields, function (acc, field) {
839 if (acc) { return acc; }
841 if (field[0] === '-') {
843 field = field.slice(1);
845 return sign * compare(a[field], b[field]);
849 completion.resolve(records);
851 if(to_get.length > 0) {
852 var rpc_promise = this._super(to_get, fields, options).done(function(records) {
853 _.each(records, function(record, index) {
854 var id = to_get[index];
855 var cached = _.detect(self.cache, function(x) {return x.id === id;});
857 self.cache.push({id: id, values: record});
859 // I assume cache value is prioritary
860 cached.values = _.defaults(_.clone(cached.values), record);
865 $.when(rpc_promise).fail(function() {completion.reject();});
869 return completion.promise();
872 * Invalidates caching of a record in the dataset to ensure the next read
873 * of that record will hit the server.
875 * Of use when an action is going to remote-alter a record which will then
876 * need to be reloaded, e.g. action button.
878 * @param {Object} id record to remove from the BDS's cache
880 evict_record: function (id) {
881 for(var i=0, len=this.cache.length; i<len; ++i) {
882 var record = this.cache[i];
883 // if record we call the button upon is in the cache
884 if (record.id === id) {
885 // evict it so it gets reloaded from server
886 this.cache.splice(i, 1);
891 call_button: function (method, args) {
892 this.evict_record(args[0][0]);
893 return this._super(method, args);
895 exec_workflow: function (id, signal) {
896 this.evict_record(id);
897 return this._super(id, signal);
899 alter_ids: function(n_ids) {
901 this.trigger("dataset_changed", n_ids);
904 instance.web.BufferedDataSet.virtual_id_regex = /^one2many_v_id_.*$/;
906 instance.web.ProxyDataSet = instance.web.DataSetSearch.extend({
908 this._super.apply(this, arguments);
909 this.create_function = null;
910 this.write_function = null;
911 this.read_function = null;
912 this.default_get_function = null;
913 this.unlink_function = null;
915 read_ids: function (ids, fields, options) {
916 if (this.read_function) {
917 return this.read_function(ids, fields, options, this._super);
919 return this._super.apply(this, arguments);
922 default_get: function(fields, options) {
923 if (this.default_get_function) {
924 return this.default_get_function(fields, options, this._super);
926 return this._super.apply(this, arguments);
929 create: function(data, options) {
930 if (this.create_function) {
931 return this.create_function(data, options, this._super);
933 return this._super.apply(this, arguments);
936 write: function (id, data, options) {
937 if (this.write_function) {
938 return this.write_function(id, data, options, this._super);
940 return this._super.apply(this, arguments);
943 unlink: function(ids) {
944 if (this.unlink_function) {
945 return this.unlink_function(ids, this._super);
947 return this._super.apply(this, arguments);
952 instance.web.CompoundContext = instance.web.Class.extend({
954 this.__ref = "compound_context";
955 this.__contexts = [];
956 this.__eval_context = null;
958 _.each(arguments, function(x) {
962 add: function (context) {
963 this.__contexts.push(context);
966 set_eval_context: function (eval_context) {
967 this.__eval_context = eval_context;
970 get_eval_context: function () {
971 return this.__eval_context;
974 return instance.web.pyeval.eval('context', this, undefined, {no_user_context: true});
978 instance.web.CompoundDomain = instance.web.Class.extend({
980 this.__ref = "compound_domain";
982 this.__eval_context = null;
984 _.each(arguments, function(x) {
988 add: function(domain) {
989 this.__domains.push(domain);
992 set_eval_context: function(eval_context) {
993 this.__eval_context = eval_context;
996 get_eval_context: function() {
997 return this.__eval_context;
1000 return instance.web.pyeval.eval('domain', this);
1004 instance.web.DropMisordered = instance.web.Class.extend({
1006 * @constructs instance.web.DropMisordered
1007 * @extends instance.web.Class
1009 * @param {Boolean} [failMisordered=false] whether mis-ordered responses should be failed or just ignored
1011 init: function (failMisordered) {
1012 // local sequence number, for requests sent
1014 // remote sequence number, seqnum of last received request
1016 this.failMisordered = failMisordered || false;
1019 * Adds a deferred (usually an async request) to the sequencer
1021 * @param {$.Deferred} deferred to ensure add
1022 * @returns {$.Deferred}
1024 add: function (deferred) {
1025 var res = $.Deferred();
1027 var self = this, seq = this.lsn++;
1028 deferred.done(function () {
1029 if (seq > self.rsn) {
1031 res.resolve.apply(res, arguments);
1032 } else if (self.failMisordered) {
1035 }).fail(function () {
1036 res.reject.apply(res, arguments);
1039 return res.promise();
1045 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: