0

Anyone can explain me this javascript/ajax code? Im using this code to FileManager (with jsTree).

this.files;
this.file_container;

var obj = this;

$.ajax
({
    url: 'ajax_requests.php?link=allFile',
    success: function (result)
    {
        obj.construct(JSON.parse(result));
    }
});

this.construct = function construct (files)
{
    this.files = files;
    this.file_container = $('#file_container');

    this.listFiles(this.files, this.file_container);
};
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • is this code alone in a file or is this a fragment ? – Nelson Teixeira Mar 02 '17 at 15:27
  • It seems to be part of an object/module that makes an ajax call for a json file, then sets the files and files_container properties of itsself to the result using itas own construct method that is added below the ajax call. When finished, it executes its own listFiles function using those properties as parameters. The top two lines, `this.files` and `this.file_container` don't actually do anything. – Shilly Mar 02 '17 at 15:34

1 Answers1

0

Well, I'm supposing this code is a fragment of a module. If it's alone in a file the "this." won't make much sense.

this.files;              // these 2 lines declarations of properties of 
this.file_container;     // the module. They aren't necessary once the 
                         // properties are created on first assign, 
                         // but it could be justa way of make code 
                         // more clear/readable

var obj = this;

$.ajax // this is an ajax call to...
({
    url: 'ajax_requests.php?link=allFile',   // ... this url ...
    success: function (result) //...which calls this function on success...
    {
        obj.construct(JSON.parse(result)); 
        //...then, as obj = this, calls contruct method with the JSON parsed 
        // ajax's call result. So the ajax call returns a JSON that is 
        // transformed to object by the JSON.parse method. Then this object is 
        // used as parameter to construct method.

    }
});

this.construct = function construct (files)
{
    this.files = files; 
    // assigns previous declared files property to the result of ajax call
    this.file_container = $('#file_container'); 
    // assigns previous declared file_container property to the jscript object
    // representing the DOM element whose id is 'file_container'.

    this.listFiles(this.files, this.file_container); 
    // calls a method called listFiles (which is not present in this fragment), 
    //having as parameters the 2 properties files and file_container.
};

In case you don't know what AJAX is, check this: What is AJAX, really?

Community
  • 1
  • 1
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73