0

I am trying to assign node data to a temporary variable within an context menu variable as

addToTable:{
                        // The "aggregate" menu item
                        label: "Show in table",
                        "seperator_before": false,
                        "seperator_after": true,
                        action: function(obj) {
                            tempNode = new Object();
                            tempNode.id =  node.id;
                            tempNode.original =  node.original;
                            //tempNode = jQuery_1_11('#ajax').jstree(true).get_node(node);
                            node.original.metadata.repeatme = true;
                            jQuery_1_11("#ajax").trigger("changed.jstree", {});
                             // or   jQuery_1_11("#ajax").trigger("changed.jstree", node);
                             // or   jQuery_1_11("#ajax").trigger("changed.jstree", tempNode );
                        }
                    },

after the trigger, if i use as follows

tempNode.original.metadata.sort_no = 2;

it also changes the "sort_no" attribute of original node.

I don't want original node data to be changed.

Thanks in advance.

kasim badami
  • 196
  • 1
  • 4
  • 14
  • 1
    you tried clone? https://api.jquery.com/clone/ – daremachine Apr 08 '15 at 13:24
  • It's not very clear what you're trying to achive. Cant you just create a new variable to hold value for your `tempNode.original.metadata.sort_no` ? – IndieTech Solutions Apr 08 '15 at 13:25
  • if you call trigger on original node so change original node not tempNode – daremachine Apr 08 '15 at 13:26
  • thanks @daremachine for your hint . clone only works on DOM elements. if you want to clone objects you need to use var newObject = jQuery.extend(true, {}, oldObject); http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object – kasim badami Apr 08 '15 at 13:34
  • I Think you need to rephrase your question. what do want to achieve with `Show in table` ? do you want to call another function(). I think you can resolve your issue if you look at it from a different prospective – IndieTech Solutions Apr 08 '15 at 13:40

1 Answers1

0

It's because in Javascript you can pass by value or reference. So, with tempNode.original = node.original you are assigning a reference to node.original rather than creating a new object. There is a whole other discussion on doing this but depending on which environment you are working on (Node, browsers, etc.) there are libraries available to help with this.

Community
  • 1
  • 1
Jason Cust
  • 10,743
  • 2
  • 33
  • 45