0
paper.on('cell:pointerdown',
    function(cellView, evt, x, y) {
       for(var i=0; i<menu.length;i++){
           if(menu[i].id===cellView.model.id){
               graph.addCell(menu[i].clone());
           }
       }
    }
);

In this code, I clone an element from an array of elements menu. I expected the clone to have a unique id because the clone function was supposed to give a new id, but it has the same id as the cloned element . Can someone explain me why does it happens and how to properly clone elements?

indubitablee
  • 8,136
  • 2
  • 25
  • 49
Pfeiffer
  • 160
  • 15
  • Why would you expect that? There's no code doing it. – isherwood Sep 24 '15 at 18:42
  • the .clone() code from jointjs do that. – Pfeiffer Sep 24 '15 at 18:45
  • "clone link.clone() Returns a new instance of the link with identical attributes." ID is an attribute. http://jointjs.com/api#joint.dia.Link:clone – isherwood Sep 24 '15 at 18:54
  • And a new id. Piece of the jointjs source code: – Pfeiffer Sep 24 '15 at 18:55
  • "/* We don't want the clone to have the same ID as the original.*/ * clone.set('id', joint.util.uuid(), { silent: true }); clone.set('embeds', '');" *modified the coment to show begin and end. – Pfeiffer Sep 24 '15 at 18:55
  • That code is using a unique string as the ID. Yours doesn't. – isherwood Sep 24 '15 at 18:55
  • JointJS indeed creates a new ID inside the clone() method. It should never happen that the ID of the clone is the same as the ID of the original. Where do you check whether the IDs are the same? Can you change your code to: `console.log(menu[i].id); var clone = menu[i].clone(); console.log(clone.id); graph.addCell(clone)` and post the answer here? – dave Sep 25 '15 at 17:11
  • Dave, after some time it started to work properly. So I suppose it's either an IDE bug or a bug on the browser. Probably the browser option as I had over 2G of memory leak on RAM at that time. I have seen you answer jointjs and svg questions before. Your answers are very good. If its possible, could you look at this other question? http://stackoverflow.com/q/32794891/4438220 – Pfeiffer Sep 26 '15 at 07:58

1 Answers1

1

it clones it as it should, you need to give the cloned element another id. something like

graph.addCell(menu[i].clone().attr('id', newNumber));
indubitablee
  • 8,136
  • 2
  • 25
  • 49
  • That does not answer the question. The clone method from jointjs should give a new id without the need of setting the new id. – Pfeiffer Sep 24 '15 at 18:49