This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Removing vObjects

Hi guys,

What is the correct way to remove all vObjects (except the first vObject) from a specific layer
on a CanvasViewer using javascript?

I'm finding that every second object I try to remove is null for some reason...



// This function removes all vObjects starting from index 1 from the 3rd layer of the CanvasViewer

function RemoveAll() {
 var canvasViewer = $find("<%= photoLabel.CanvasViewer.ClientID %>");
 var cv = canvasViewer.get_canvas();
 var layers = canvasViewer.get_canvas().get_layers();
 var voc = layers.get_item(2).get_vObjects();

 for (var i = 1; i <= voc.get_count(); i++) { 
     voc.removeAt(i);
}



In one example, I have 7 objects on the 3rd layer. I want to remove all vObjects except the first one (index 0).
In this example there will be 6 vObjects I need to remove.

When I run the code above only every second vObject is removed for some reason.

I tried to debug the issue by putting an alert before the for-loop:


alert(voc.get_count()); // this displays 7 in my example


I also put an alert before voc.removeAt(i)...


alert(voc.get_item(i).get_index());


...I find that only 3 objects exist, the other 3 are always null.

Any idea what might be causing this, or if there is a better way to achieve the above requirement?

Thanks!
Andrew
Hi Andrew!

Sorry for my late reply.
I discussed your issue with our developers. They provided me with the code example which might be helpful:
$(".psdEditor button.delete").live('click', function () {
   if (!confirm(self._confirmDeleteText)) return false;
   var vObjects = ($(this).closest('#imageInputList').size() > 0 ? self.getCurrentLayerImage() : self.getCurrentLayerText()).get_vObjects();
   var id = $(this).closest('li').attr('id');
   for (var k = 0; k < vObjects.get_count(); k++) {
    var item = vObjects.get_item(k);
    if (item.get_uniqueId() == id) {
     vObjects.removeAt(vObjects.indexOf(item));
     break;
    };
   }
   self._canvas.set_currentVObjectIndex(-1);
   self._canvas.redraw();
   self._onZoneChange();
   $('.psdEditor').trigger('userImageChanged', []);
   return false;
  });

They say the attention should be paid on this line:
vObjects.removeAt(vObjects.indexOf(item));

This is the correct way to delete vObjects.
Thanks Pauline,

The code provided is not what I require.
Please ask your developers to provide an example of removing vObjects from a specific CanvasViewer layer.

I am using the .removeAt method already and notice that not all vObjects are being removed. I would like your developers to clarify whether my implementation is incorrect or whether this is a bug with the CanvasViewer.

Thanks,
Andrew
Hi Andy,

You don't consider the fact that the index of the objects standing after the one you delete is decreased by 1. Please try the following code:
function RemoveAll() {
 var canvasViewer = $find("<%= photoLabel.CanvasViewer.ClientID %>");
 var cv = canvasViewer.get_canvas();
 var layers = canvasViewer.get_canvas().get_layers();
 var voc = layers.get_item(2).get_vObjects();
 
 while(voc.get_count() > 1)
{
    voc.removeAt(voc.get_count() - 1);
}

cv.set_currentVObjectIndex(-1);
cv.redraw();
}

Thanks very much. The code worked perfectly.