[FIX] Simplified dataset. Plugged form view.

bzr revid: fme@openerp.com-20110404150619-z8tc382ujdfw89ji
This commit is contained in:
Fabien Meghazi 2011-04-04 17:06:19 +02:00
parent 1865a1dae0
commit 6dd76ea8f6
4 changed files with 92 additions and 219 deletions

View File

@ -303,10 +303,10 @@ class DataSet(openerpweb.Controller):
return Model.read(ids, fields or False)
@openerpweb.jsonrequest
def get(self, request, model, ids):
self.do_get(request, model, ids)
def get(self, request, model, ids, fields=False):
return self.do_get(request, model, ids, fields)
def do_get(self, request, model, ids):
def do_get(self, request, model, ids, fields=False):
""" Fetches and returns the records of the model ``model`` whose ids
are in ``ids``.
@ -323,7 +323,7 @@ class DataSet(openerpweb.Controller):
:rtype: list
"""
Model = request.session.model(model)
records = Model.read(ids)
records = Model.read(ids, fields)
record_map = dict((record['id'], record) for record in records)

View File

@ -1,8 +1,7 @@
openerp.base.data = function(openerp) {
openerp.base.DataGroup = openerp.base.Controller.extend(
/** @lends openerp.base.DataGroup# */{
openerp.base.DataGroup = openerp.base.Controller.extend( /** @lends openerp.base.DataGroup# */{
/**
* Management interface between views and the collection of selected OpenERP
* records (represents the view's state?)
@ -16,10 +15,9 @@ openerp.base.DataGroup = openerp.base.Controller.extend(
}
});
openerp.base.DataSet = openerp.base.Controller.extend(
/** @lends openerp.base.DataSet# */{
openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base.DataSet# */{
/**
* Management interface between views and the collection of selected
* DateaManagement interface between views and the collection of selected
* OpenERP records (represents the view's state?)
*
* @constructs
@ -32,13 +30,14 @@ openerp.base.DataSet = openerp.base.Controller.extend(
this._super(session);
this.model = model;
this._ids = [];
this._active_ids = null;
this._active_id_index = 0;
this.ids = [];
this.offset
this.index = 0;
this.count = 0;
this._sort = [];
this._domain = [];
this._context = {};
this.sort = [];
this.domain = [];
this.context = {};
},
start: function() {
},
@ -55,141 +54,54 @@ openerp.base.DataSet = openerp.base.Controller.extend(
* @param {Number} [limit=null] The maximum number of records to return
* @returns itself
*/
fetch: function (fields, offset, limit) {
fetch: function (fields, offset, limit, callback) {
var self = this;
offset = offset || 0;
this.rpc('/base/dataset/find', {
model: this.model,
fields: fields,
domain: this._domain,
context: this._context,
sort: this._sort,
domain: this.domain,
context: this.context,
sort: this.sort,
offset: offset,
limit: limit
}, _.bind(function (records) {
var data_records = _.map(
records, function (record) {
return new openerp.base.DataRecord(this.session, this.model, fields, record);
}, this);
this.on_fetch(data_records, {
offset: offset,
limit: limit,
domain: this._domain,
context: this._context,
sort: this._sort
});
}, this));
return this;
}, function (records) {
var r = [];
this.offset = offset;
this.count = records.length; // TODO: get real count
for (var i=0; i < records.length; i++ ) {
self.ids.push(records[i].id);
r.push(new openerp.base.DataRecord(self.session, self.model, fields, records[i]));
}
callback(r);
});
},
/**
* Fires after the DataSet fetched the records matching its internal ids selection
*
* TODO: remove?
*
* @event
* @param {Array} records An array of the DataRecord fetched
* @param event The on_fetch event object
* @param {Number} event.offset the offset with which the original DataSet#fetch call was performed
* @param {Number} event.limit the limit set on the original DataSet#fetch call
* @param {Array} event.domain the domain set on the DataSet before DataSet#fetch was called
* @param {Object} event.context the context set on the DataSet before DataSet#fetch was called
* @param {Array} event.sort the sorting criteria used to get the ids
*/
on_fetch: function (records, event) {
},
/**
* Fetch all the currently active records for this DataSet (records selected via DataSet#select)
*
* TODO: add fields, return deferred
*
* @returns itself
*/
active_ids: function (fields) {
fetch_ids: function (ids, fields, callback) {
this.rpc('/base/dataset/get', {
ids: this.get_active_ids(),
model: this.model
}, _.bind(function (records) {
this.on_active_ids(_.map(
records, function (record) {
return new openerp.base.DataRecord(this.session, this.model, fields, record);
}, this));
}, this));
return this;
model: this.model,
ids: ids,
fields: fields
}, function (records) {
var r = [];
for (var i=0; i < records.length; i++ ) {
r.push(new openerp.base.DataRecord(self.session, self.model, fields, records[i]));
}
callback(r);
});
},
/**
* Fires after the DataSet fetched the records matching its internal active ids selection
*
* TODO: remove?
*
* @event
* @param {Array} records An array of the DataRecord fetched
*/
on_active_ids: function (records) {
},
/**
* Fetches the current active record for this DataSet
*
* TODO: add field, return deferred?
*
* @returns itself
*/
active_id: function (fields) {
this.rpc('/base/dataset/get', {
ids: [this.get_active_id()],
model: this.model
}, _.bind(function (records) {
var record = records[0];
this.on_active_id(
record && new openerp.base.DataRecord(
this.session, this.model,
fields, record));
}, this));
return this;
},
/**
* Fires after the DataSet fetched the record matching the current active record
*
* @event
* @param record the record matching the provided id, or null if there is no record for this id
*/
on_active_id: function (record) {
},
/**
* Configures the DataSet
*
* @param options DataSet options
* @param {Array} options.domain the domain to assign to this DataSet for filtering
* @param {Object} options.context the context this DataSet should use during its calls
* @param {Array} options.sort the sorting criteria for this DataSet
* @returns itself
*/
set: function (options) {
if (options.domain) {
this._domain = _.clone(options.domain);
}
if (options.context) {
this._context = _.clone(options.context);
}
if (options.sort) {
this._sort = _.clone(options.sort);
}
return this;
fetch_index: function (fields, callback) {
fields = fields || false;
this.fetch_ids([this.ids[this.index]], fields, callback);
},
/**
* Activates the previous id in the active sequence. If there is no previous id, wraps around to the last one
* @returns itself
*/
prev: function () {
this._active_id_index -= 1;
if (this._active_id_index < 0) {
this._active_id_index = this._active_ids.length - 1;
previous: function () {
this.index -= 1;
if (this.index < 0) {
this.index = this.ids.length - 1;
}
return this;
},
@ -199,73 +111,13 @@ openerp.base.DataSet = openerp.base.Controller.extend(
* @returns itself
*/
next: function () {
this._active_id_index += 1;
if (this._active_id_index >= this._active_ids.length) {
this._active_id_index = 0;
this.index += 1;
if (this.index >= this.ids.length) {
this.index = 0;
}
return this;
},
/**
* Sets active_ids by value:
*
* * Activates all ids part of the current selection
*
* * Sets active_id to be the first id of the selection
*
* @param {Array} ids the list of ids to activate
* @returns itself
*/
select: function (ids) {
this._active_ids = ids;
this._active_id_index = 0;
return this;
},
/**
* Fetches the ids of the currently selected records, if any.
*/
get_active_ids: function () {
return this._active_ids;
},
/**
* Sets the current active_id by value
*
* If there are no active_ids selected, selects the provided id as the sole active_id
*
* If there are ids selected and the provided id is not in them, raise an error
*
* @param {Object} id the id to activate
* @returns itself
*/
activate: function (id) {
if(!this._active_ids) {
this._active_ids = [id];
this._active_id_index = 0;
} else {
var index = _.indexOf(this._active_ids, id);
if (index == -1) {
throw new Error(
"Could not find id " + id +
" in array [" + this._active_ids.join(', ') + "]");
}
this._active_id_index = index;
}
return this;
},
/**
* Fetches the id of the current active record, if any.
*
* @returns record? record id or <code>null</code>
*/
get_active_id: function () {
if (!this._active_ids) {
return null;
}
return this._active_ids[this._active_id_index];
}
});
openerp.base.DataRecord = openerp.base.Controller.extend({

View File

@ -21,7 +21,7 @@ openerp.base.FormView = openerp.base.Controller.extend(
this.dataset = dataset;
this.model = dataset.model;
this.view_id = view_id;
this.fields_views = {};
this.fields_view = {};
this.widgets = {};
this.widgets_counter = 0;
this.fields = {};
@ -35,7 +35,6 @@ openerp.base.FormView = openerp.base.Controller.extend(
on_loaded: function(data) {
var self = this;
this.fields_view = data.fields_view;
//this.log(this.fields_view);
var frame = new openerp.base.form.WidgetFrame(this, this.fields_view.arch);
@ -45,25 +44,25 @@ openerp.base.FormView = openerp.base.Controller.extend(
});
this.$element.find('button.form_save').click(this.do_save);
// this.dataset.on_active_id.add(this.on_record_loaded);
// this.dataset.active_id(fields of the form, this.on_record_loaded);
this.dataset.fetch_index(this.fields_view.fields, this.on_record_loaded);
},
on_next: function() {
// this.dataset.next();
// this.dataset.active_id(fields of the form, this.on_record_loaded);
this.dataset.next();
this.dataset.fetch_index(this.fields_view.fields, this.on_record_loaded);
},
on_prev: function() {
// this.dataset.prev();
// this.dataset.active_id(fields of the form, this.on_record_loaded);
this.dataset.prev();
this.dataset.fetch_index(this.fields_view.fields, this.on_record_loaded);
},
on_record_loaded: function(record) {
this.datarecord = record;
for (var f in this.fields) {
this.fields[f].set_value(this.datarecord.values[f]);
if (record.length) {
this.datarecord = record[0];
for (var f in this.fields) {
this.fields[f].set_value(this.datarecord.values[f]);
}
this.on_form_changed();
this.ready = true;
}
this.on_form_changed();
this.ready = true;
},
on_form_changed: function(widget) {
for (var w in this.widgets) {
@ -101,11 +100,35 @@ openerp.base.FormView = openerp.base.Controller.extend(
// rpc - save.callbacl on_saved
}
},
do_search: function() {
if (!this.ready) {
return false;
}
var invalid = false;
var values = {};
for (var f in this.fields) {
f = this.fields[f];
if (f.invalid) {
invalid = true;
} else {
values[f.name] = f.value;
}
}
if (invalid) {
this.on_invalid();
} else {
console.log("Save form", values);
// TODO: save values via datarecord
// rpc - save.callbacl on_saved
}
},
on_invalid: function() {
},
on_saved: function() {
// Check response for exceptions, display error
},
do_search: function (domains, contexts, groupbys) {
},
on_action: function (action) {
}
});

View File

@ -45,8 +45,7 @@ openerp.base.ListView = openerp.base.Controller.extend({
}
}
this.dataset.fields = this.cols;
this.dataset.on_fetch.add(this.do_fill_table);
var width = this.$element.width();
this.$table.jqGrid({
datatype: "local",
@ -85,10 +84,9 @@ openerp.base.ListView = openerp.base.Controller.extend({
group_by_seq: groupbys
}, function (results) {
// TODO: handle non-empty results.group_by with read_group
self.dataset.set({
context: results.context,
domain: results.domain
}).fetch(self.fields_view.fields, 0, self.limit);
self.dataset.context = results.context;
self.dataset.domain = results.domain;
self.dataset.fetch(self.dataset.fields, 0, self.limit, self.do_fill_table);
});
}
});