General builder support

Web UI mainly uses declarative way of defining UI structure. When a new object type is created it is often required to create a new builder which would build the objects from spec file. The builders' logic is mostly the same. This patch adds a general builder with some extendability capabilities.

Now it is possible to:
  1) define spec for single object and build it by calling IPA.build(spec, /* optional */ builder_fac)
  2) define an array of specs and build the objects by the same call

Prerequisite for following action list patches.

https://fedorahosted.org/freeipa/ticket/2707
This commit is contained in:
Petr Vobornik 2012-04-04 16:23:16 +02:00
parent 58732a83bc
commit 12401fe4da

View File

@ -964,6 +964,73 @@ IPA.concurrent_command = function(spec) {
return that;
};
IPA.builder = function(spec) {
spec = spec || {};
var that = {};
that.factory = spec.factory || IPA.default_factory;
that.build = function(spec) {
var factory = spec.factory || that.factory;
//when spec is a factory function
if (!spec.factory && typeof spec === 'function') {
factory = spec;
spec = {};
}
var obj = factory(spec);
return obj;
};
that.build_objects = function(specs) {
var objects = [];
for (var i=0; i<specs.length; i++) {
var spec = specs[i];
var obj = that.build(spec);
objects.push(obj);
}
return objects;
};
return that;
};
IPA.build = function(spec, builder_fac) {
if (!spec) return null;
if (!builder_fac) builder_fac = IPA.builder;
var builder = builder_fac();
var product;
if ($.isArray(spec)) {
product = builder.build_objects(spec);
} else {
product = builder.build(spec);
}
return product;
};
IPA.default_factory = function(spec) {
spec = spec || {};
var that = {};
$.extend(that, spec);
return that;
};
/* helper function used to retrieve information about an attribute */
IPA.get_entity_param = function(entity_name, name) {