Please tell me if the received JSON object can be stored after its first processing by the handler, so that at some event this object is available to me (as a global one). Can it make an object visible while the page is not overloaded?

  • one
    I would reconsider the idea of ​​the object's globality, it is better to make it visible only for the event handler. - zb '

4 answers 4

var data_storage; ... function(data){ var obj=JSON.parse(data); ... // вносятся изменения в obj data_storage=JSON.stringify(obj); } ... var some_obj=JSON.parse(data_storage); // получение "копии" объекта 
  • Why? Output to console JSON.parse (data_storage) .c - kaf
  • although ... "after its first processing" ... well, then it works, just a strange idea .... why not just store the object - zb '10
  • Here a difficult philosophical question arises: what is the primary processing, if data[0]=function(){} then it will not work to clone through json, it may not work at all, since complete cloning in json has some difficulties. - zb '
  • one
    you should not dig so deeply, most likely we are talking about changes only in those properties that are supported in the transfer via JSON - kaf

handler.getJSON - get and save to an object property - object obtained from JSON

 handler={ data:null, getJSON:function(){ // ... получаем и парсим JSON; } } console.log( 
  • and do you think that data = will change you are deeply mistaken. Moreover, getJSON will not execute at all in such a construction, it is just an announcement. and even so - zb '10
  • I am not mistaken, I was sealed up - Gedweb
  • Yes, no, you are mistaken, the getJSON call will not happen. besides, there ajax is the asynchronous request by default, immediately after the call (even if you do handler.getJSON() ), the data cannot be used. this is why I am talking about the need to initialize an event handler in a callback request. - zb '
  • @eicto, the fact that after calling handler.getJSON () an object will appear in And where to call the method and then take the property will be decided by the author - Gedweb
  • @ minusator: comment out - Gedweb

It is necessary to clone an object, since we are obviously enumerable, we can use JSON.* without any fear JSON.* :

 $.get(url,function (data) { data[0]=""; //первое изменение data var stored = JSON.parse(JSON.stringify(data)); //клонируем data[1]=""; //другое изменяющие data console.log(data); // вывод измененной data $('#clickme').click(function () { console.log(stored); //вывод оригинальной data }); }, "json"); 

Well, as I said above, it is better not to make global objects without super-necessity. so, if you embed not enumerable properties into an object during preprocessing, then you will not be able to clone such an object using honest methods. Examples of this, function (), DOM objects, etc., say



 data[0]=$('#mydiv'); data[1]=$('#mydiv').get(0); 


as @Gedweb correctly noted, it is assumed that this request will be executed once, in the case of a multiple request, you must either do .unbind() or keep the variable in scope for above $.get()

  • every time $.get on #clickme handler will be #clickme . Thus, clicking on this object will perform several iterations. Use unbind . PS As you can see, your code is not perfect, I wrote it so that you would hold back your fervor in criticizing others. - Gedweb
 // app namespace var app = function() { var jsData; function get(){ return jsData; } function set(data){ jsData = data; } function printConsole(str) { console.log(str +'>>> jsData = '+ JSON.stringify(jsData)) } return { getData:get, setData:set, printData:printConsole } }(); //... $(document).ready(function() { $.getJSON("hierarchy.php?id=2", showHierarchy); }); function showHierarchy(data) { app.printData(0); //undefined app.setData(data); app.printData(1); //object var jsData = app.getData(); //copy JSON-object to member-function and use it data console.log('Local data: '+ JSON.stringify(jsData)); } 
  • I thank all the participants for the consultations, the idea of ​​creating a global facility has been clarified. Above is my final implementation of this idea. Closed question - Sergey Pysnik