Simplify adding / referencing of JSON dictionary entries.

This commit is contained in:
Jürg Lehni 2013-02-11 19:31:58 -08:00
parent e477c49d1e
commit 15554b8bf2
3 changed files with 17 additions and 12 deletions

View file

@ -34,8 +34,10 @@ var Gradient = this.Gradient = Base.extend(/** @lends Gradient# */{
},
_serialize: function(dictionary) {
return dictionary.get(this) || dictionary.set(this, Base.serialize(
[this._type, this._stops, this.type], true, dictionary));
return dictionary.add(this, function() {
return Base.serialize([this._type, this._stops, this.type],
true, dictionary);
});
},
/**

View file

@ -259,14 +259,15 @@ this.Base = Base.inject(/** @lends Base# */{
length: 0,
definitions: {},
references: {},
get: function(item) {
return this.references['#' + item._id];
},
set: function(item, json) {
this.length++;
var id = '#' + item._id;
this.definitions[id] = json;
return this.references[id] = [id];
add: function(item, create) {
var id = '#' + item._id,
ref = this.references[id];
if (!ref) {
this.length++;
this.definitions[id] = create.call(item);
ref = this.references[id] = [id];
}
return ref;
}
};
}

View file

@ -69,8 +69,10 @@ var Symbol = this.Symbol = Base.extend(/** @lends Symbol# */{
},
_serialize: function(dictionary) {
return dictionary.get(this) || dictionary.set(this, [this._type,
Base.serialize(this._definition, false, dictionary)]);
return dictionary.add(this, function() {
return Base.serialize([this._type, this._definition],
false, dictionary);
});
},
// TODO: Symbol#remove()