[IMP] ListView#save_row internals by taking advantage of Deferred#pipe

and FormView#do_save returning a deferred of course.

bzr revid: xmo@openerp.com-20111208123846-dp3cgbsljl41q82k
This commit is contained in:
Xavier Morel 2011-12-08 13:38:46 +01:00
parent 37dee90724
commit 54f3f38957
2 changed files with 27 additions and 31 deletions

View File

@ -423,23 +423,22 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
self.on_invalid(); self.on_invalid();
return $.Deferred().reject(); return $.Deferred().reject();
} else { } else {
var save_deferral;
if (!self.datarecord.id) { if (!self.datarecord.id) {
openerp.log("FormView(", self, ") : About to create", values); openerp.log("FormView(", self, ") : About to create", values);
return self.dataset.create(values).pipe(function(r) { save_deferral = self.dataset.create(values).pipe(function(r) {
return self.on_created(r, undefined, prepend_on_create); return self.on_created(r, undefined, prepend_on_create);
}).then(success); }, null);
} else if (_.isEmpty(values)) { } else if (_.isEmpty(values)) {
openerp.log("FormView(", self, ") : Nothing to save"); openerp.log("FormView(", self, ") : Nothing to save");
if (success) { save_deferral = $.Deferred().resolve({}).promise();
success({});
}
return $.Deferred().resolve({}).promise();
} else { } else {
openerp.log("FormView(", self, ") : About to save", values); openerp.log("FormView(", self, ") : About to save", values);
return self.dataset.write(self.datarecord.id, values, {}).pipe(function(r) { save_deferral = self.dataset.write(self.datarecord.id, values, {}).pipe(function(r) {
return self.on_saved(r); return self.on_saved(r);
}).then(success); }, null);
} }
return save_deferral.then(success);
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);

View File

@ -303,30 +303,27 @@ openerp.web.list_editable = function (openerp) {
save_row: function () { save_row: function () {
//noinspection JSPotentiallyInvalidConstructorUsage //noinspection JSPotentiallyInvalidConstructorUsage
var self = this, done = $.Deferred(); var self = this, done = $.Deferred();
this.edition_form.do_save(function (result) { return this.edition_form
if (result.created && !self.edition_id) { .do_save(null, this.options.editable === 'top')
self.records.add({id: result.result}, .pipe(function (result) {
{at: self.options.editable === 'top' ? 0 : null}); if (result.created && !self.edition_id) {
self.edition_id = result.result; self.records.add({id: result.result},
} {at: self.options.editable === 'top' ? 0 : null});
var edited_record = self.records.get(self.edition_id); self.edition_id = result.result;
}
var edited_record = self.records.get(self.edition_id);
$.when( return $.when(
self.handle_onwrite(self.edition_id), self.handle_onwrite(self.edition_id),
self.cancel_pending_edition().then(function () { self.cancel_pending_edition().then(function () {
$(self).trigger('saved', [self.dataset]); $(self).trigger('saved', [self.dataset]);
})).then(function () { })).pipe(function () {
done.resolve({ return {
created: result.created || false, created: result.created || false,
edited_record: edited_record edited_record: edited_record
}); };
}, function () { }, null);
done.reject(); }, null);
});
}, this.options.editable === 'top').fail(function () {
done.reject();
});
return done.promise();
}, },
/** /**
* If the current list is being edited, ensures it's saved * If the current list is being edited, ensures it's saved