Here he created a kind of class, actually an object with functions. So he can centralize what the projeto
can make and use anywhere else.
var projeto = {
methods: {
funcao1: function() {
},
funcao2: function() {
},
init: function() {
this.funcao1(), this.funcao2()
},
init_ajax: function() {}
},
ajax: function() {
return this.methods.init_ajax()
},
mounted: function() {
return this.methods.init()
}
};
That $(document)
takes the document itself, which is read by the browser. What it does with the .ready()
and ajaxStop()
is to pick up the events triggered by Document at the times the document is read and some ajax call is terminated, respectively.
With this, he defined methods that must be performed at these times.
When document is read (page loaded) run projeto.mounted()
$(document).ready(function() {
projeto.mounted()
})
When ajax loading stops, run projeto.ajax()
$(document).ajaxStop(function() {
projeto.ajax()
});
Realize that projeto
is the variable that has the methods ajax
and mounted
.
You probably already used something like element.on('click')
or element.click()
to capture button clicks or something, the process here is the same, however, instead of capturing the click, you are capturando
other things. It could be a Hover, a keyup, etc.
I will list some links here that will help in general understanding, your doubt and good and will help a lot people.
- Working with Javascript events
- Organizing Your Javascript Code
- About the Document.ready
I may be wrong, so I’d rather just comment.. I believe that dev did it because it was simply more practical for him and tried to create a pattern of his own, you know?
– David Dias
@So that would be more of an organizational pattern? and that final part of the code what is its function?
– Josimara
I tried to explain piece by piece in response to see if it helps you
– David Dias