Use entities module for entity registration, build and holding

https://fedorahosted.org/freeipa/ticket/3235
This commit is contained in:
Petr Vobornik 2013-04-12 19:50:28 +02:00
parent a901f8b0e7
commit 86f0b5eb64
3 changed files with 32 additions and 83 deletions

View File

@ -39,6 +39,18 @@ define(['./_base/Singleton_registry'], function(Singleton_registry) {
* entities.get('entity_name');
*
*/
var entities = new Singleton_registry();
var init = function(entity, spec, context) {
if (entity.init) {
entity.init(spec, context);
}
return entity;
};
entities.builder.post_ops.push(init);
return entities;
});

View File

@ -41,7 +41,6 @@ IPA.entity = function(spec) {
that.defines_key = spec.defines_key !== undefined ? spec.defines_key : true;
that.metadata = spec.metadata;
that.builder = spec.builder;
that.dialogs = $.ordered_map();
that.dialog_specs = spec.dialogs || [];
@ -180,41 +179,21 @@ IPA.entity = function(spec) {
return that;
};
that.builder = spec.builder || IPA.entity_builder(that);
that.entity_init = that.init;
return that;
};
IPA.entity_builder = function() {
IPA.entity_builder = function(entity) {
var that = IPA.object();
var entity = null;
var facet_group = null;
var facet = null;
var section = null;
that.entity = function(spec) {
var factory = IPA.entity;
if (spec instanceof Object) {
factory = spec.$factory || IPA.entity;
} else {
spec = { name: spec };
}
spec.builder = that;
entity = factory(spec);
that.facet_groups([
'member',
'settings',
'memberof',
'managedby'
]);
return that;
};
that.facet_group = function(spec) {
spec.entity = entity;
if (spec instanceof Object) {
@ -427,9 +406,14 @@ IPA.entity_builder = function() {
return that.dialog(spec);
};
that.build = function(){
return entity;
};
that.facet_groups([
'member',
'settings',
'memberof',
'managedby'
]);
return that;
};

View File

@ -27,8 +27,9 @@ define(['./jquery',
'./_base/Builder',
'./_base/i18n',
'./_base/metadata_provider',
'./entities',
'./text'],
function($, JSON, Builder, i18n, metadata_provider, text) {
function($, JSON, Builder, i18n, metadata_provider, entities, text) {
var IPA = function() {
@ -220,64 +221,16 @@ var IPA = function() {
};
that.register = function(name, factory) {
that.remove_entity(name);
that.entity_factories[name] = factory;
};
that.create_entity = function(name) {
var factory = that.entity_factories[name];
if (!factory) return null;
try {
var builder = IPA.entity_builder();
builder.entity({
$factory: factory,
name: name
});
var entity = builder.build();
entity.init();
return entity;
} catch (e) {
if (e.expected) {
/*expected exceptions thrown by builder just mean that
entities are not to be registered. */
return null;
}
if (e.message) {
alert(e.message);
} else {
alert(e);
}
return null;
}
};
that.get_entities = function() {
return that.entities.values;
entities.remove(name);
entities.register({
type: name,
factory: factory,
spec: { name: name }
});
};
that.get_entity = function(name) {
if (typeof name === 'object') return name;
var entity = that.entities.get(name);
if (!entity) {
entity = that.create_entity(name);
if (entity) that.add_entity(entity);
}
return entity;
};
that.add_entity = function(entity) {
that.entities.put(entity.name, entity);
};
that.remove_entity = function(name) {
that.entities.remove(name);
return entities.get(name);
};
that.display_activity_icon = function() {