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?
- oneI would reconsider the idea of ββthe object's globality, it is better to make it visible only for the event handler. - zb '
4 answers
var data_storage; ... function(data){ var obj=JSON.parse(data); ... // Π²Π½ΠΎΡΡΡΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ Π² obj data_storage=JSON.stringify(obj); } ... var some_obj=JSON.parse(data_storage); // ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ "ΠΊΠΎΠΏΠΈΠΈ" ΠΎΠ±ΡΠ΅ΠΊΡΠ°
- will not work - zb '
- 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 ' - oneyou 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
handler.data
- object obtained from JSON
handler={ data:null, getJSON:function(){ // ... ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ ΠΈ ΠΏΠ°ΡΡΠΈΠΌ JSON this.data=JSON.parse(string); } } console.log(handler.data)
- and do you think that data = will change handler.data? you are deeply mistaken. Moreover, getJSON will not execute at all in such a construction, it is just an announcement. jsfiddle.net/oceog/HnkLH 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 handler.data. 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]=function();
or
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. Useunbind
. 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