0

I have an angularjs project which retrieves JSON files from a server and uses the contents to display the data in the screen.

I'm using a service to load the data, and this service calls the server for a new JSON file every 2 seconds (I removed that from the code below for simplicity).

var data = $resource(:file.json', {}, {
        query: {method: 'GET', params: {file: '@file'}}
    });

this.load = function(file, myFunction) {
    data.query({file:file}, function(data) {
        myFunction(data);               
    }
} 

Now, these files can be really big and sometimes there's no need to process the file because there are no changes from the previous one received. I have a property in the JSON file with the version number, and I should not process the file unless that version number is higher than the one in the previous file.

I can do that by calling the query service, which loads the file contents into a js object and then check the version, if the file is really big it might take a while to load it. Is there a way to access that property value (version) ONLY and then, depending on it, load the file into a js object?

EDIT: The thing that I'm guessing is that loading a 1MB JSON file to check a version number inside it might take a while (or maybe no and that $resource action is really fast, anyone knows?), but I'm not really sure that it can be done any other way, as I'm checking a specific property inside the file.

Many thanks in advance.

David
  • 3,364
  • 10
  • 41
  • 84

2 Answers2

0

Do you have access to the json files?

I'm not sure how you generate your json files but you could try adding the version number in the filename and check if a newer filename exists. I have not tested this but maybe it's worth a try.

jvjefke
  • 21
  • 5
0

HTML5 and Javascript now provides a File API which can be used to read the file line by line. You can find information regarding this feature here:

http://www.html5rocks.com/en/tutorials/file/dndfiles/

This will slice the full file into string and take just the first line(asuming the version is in there)

data.substr(0, data.indexOf("\n"));

--

Bonus:

Also in this answer you will find out how to read the first line of a file:

https://stackoverflow.com/a/12227851/2552259

var XHR = new XMLHttpRequest();
XHR.open("GET", "http://hunpony.hu/today/changelog-en.txt", true);
XHR.send();
XHR.onload = function (){
    console.log( XHR.responseText.slice(0, XHR.responseText.indexOf("\n")) );
};

Another question with the same topic:

https://stackoverflow.com/a/6861246/2552259

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://website.com/file.txt", true);
txtFile.onreadystatechange = function()
{
  if (txtFile.readyState === 4) {  // document is ready to parse.
    if (txtFile.status === 200) {  // file is found
      allText = txtFile.responseText; 
      lines = txtFile.responseText.split("\n");
    }
  }
}
txtFile.send(null);
Community
  • 1
  • 1
Jorge de los Santos
  • 4,583
  • 1
  • 17
  • 35
  • The first example assumes that the contents are already loaded in variable 'data', and that is what I'm trying to avoid, load the whole file to read something. I could include the version as the first property in the JSON so that I could start reading the first lines until I find it, but not sure if that will be faster than what I have, I'll try to do some tests. – David May 18 '15 at 15:13
  • Then use the HTML api to read the bytes you need from the file. First link. – Jorge de los Santos May 18 '15 at 16:03
  • I might have to change my JSON files a bit, but this can do the trick. Thanks. – David May 19 '15 at 06:10
  • Actually, it seems the HTML API also reads the file first; in the first example is loading the file and the slicing the response to get only the first line, but even then is extremely faster than reading a json file into a js object, so this is worth it. – David May 19 '15 at 06:46