I have an Object that describes my document. Each document contains layers, that described in Objects in an array
var example = {
id: 'some_id',
layers: [{
id: 'some_layer_id',
width: 100,
height: 200
}]
};
There are different types of layers, each has it's template:
var layer_templates = [{
type: 'text',
width: 100,
height: 100,
style: 'common'
}];
I create new layers in jQuery event:
jQuery('#button').on('click', function () {
var ldata = layer_templates[0]; // get a template
ldata['id'] = 's-l-' + Math.random().toString(); // create an id
example['layers'].push(ldata); // add new "layer" to document
console.log(example['layers']); // check
});
Every time i add new ldata
layer to example['layers']
array, it keeps only initial object with id: 'some_layer_id'
and rewrites all newly created objects with the last one.
Please check jsFiddle:Problem in action