I have implemented two form binding specifically to bind a complex object to a complex form. The heavily lifting is performed by the following mixin:
Code:
Ext.define('GenPresWebView.controller.mixin.bind.CanBindToForm', {
bindingToForm: function (form, binding) {
var me = this, b, bfield,
bfields = me.ext.ComponentQuery.query('component[bindingName]', form);
for (b in binding) {
if (_.has(binding, b) && binding[b].value !== undefined) {
bfield = _.find(bfields, function (bf) { return bf.bindingName === b; } );
if (!bfield) Ext.Error.raise('cannot bind to field');
bfield[binding[b].setter].call(bfield, binding[b].value);
}
}
},
formToBinding: function (form, binding) {
var me = this, b, bfield,
bfields = me.ext.ComponentQuery.query('component[bindingName]', form);
for (b in binding) {
if (_.has(binding, b)) {
bfield = _.find(bfields, function (bf) { return bf.bindingName === b; } );
if (!bfield || !bfield[binding[b].getter]) console.debug(b);
if (bfield && bfield[binding[b].getter]) binding[b].value = bfield[binding[b].getter].call(bfield);
}
}
return binding;
}
});
The form has components (can be anything from a simple text field to a complex field set with combo boxes etc) and these components have setters and getters. First, you get a list of all components that have a binding name, secondly find the corresponding binding and call use the getter and setter defined by that binding to get or set the form field. The mapping from the business object to the binding object is performed by the controller.