70001209 How to update an XProperty

Article 70001209
Type HowTo
Product WebJS
Date Added 10/31/2017 12:00:00 AM
Fixed 7.7014.0.1 (10/31/2017 12:00:00 AM)
Submitted by Brendan Fry

Summary

How can i update an XProperty and not add a new one with the same name and different value

Solution

Here is an example where we search in mousedown event for a specific XProperty of an entity and if we find it we change it’s value,if not we add a new one.

      function _vdmousedown(e) {
            var entity = e.target.GetEntityFromPoint(e.xPix, e.yPix);
            if (entity.XProperties === undefined) {// in case there are not any XProperties we add the new that I want
                var newExProp = vdcanvas.AddXProperty(entity, 'name1', 5.0);
            }
            else {
                for (var i = 0; i < entity.XProperties.Items.length; i++) {//search for the XProperty we want by it’s name
                    if (entity.XProperties.Items[i].Name == "name1") {
                        var xprop = vdcanvas.GetXProperty(entity, "name1");//If we find the XProperty name that we search for, we change its value 
                        xprop.PropValue = 'newValue';                     
                    }
                    else {
                        if (newExProp) return;
                        var newExProp = vdcanvas.AddXProperty(entity, 'name1', 5.0); // if we didn’t find the Xproperty that we were searching for in the XProperties of the entity,we add a new one                      
                    }
                }
            }
      }

Send comments on this topic.