I am passing a JSON object as a reference to my custom jquery widget like this:
var jsonData = { "Name": "John ", "Country": "US" };
$("#testdiv").TestWidget({MyData:jsonData});
And in my widget, I am changing the data like this:
$.widget("my.TestWidget", {
options: {
MyData: null
},
_create: function () {
this.options.MyData.Name = "NewName";
}
});
But, the change is not reflected in the original JSON data because apparently jQuery is creating a copy of my JSON data when creating the widget!
This is very undesirable. Is there any way to prevent copying of data and instead pass the object as reference?
Above is a simplified version of our original scenario.
Thanks
I’m afraid this is not possible. Looking at the jQuery UI source code, you can see why:
The
optionsis being copied using the$.extendmethod.Personally, I don’t see a reason why you would want to pass
jsonDataas a reference here. It seems to me that there’s something wrong with your design. Could you provide more information on that matter?