For a long time I am looking for a practical (reusable, clear, updateable,...) solution to the following:
Although everything works fine I am not satisfied with the way the JS is used. Here are some major issues:
What I would like to ask you is your opinion/advice on using Javascript in larger php frameworks.
Recently I was thinking about writing a separate method in the widget class (let's say function Script()) where I'll put all the JS code and then walk through all the widgets, take all the JS code and put it in one .js file which will be used until there is a change made in one of the widgets. What do you think about this approach?
Thanks!
Closures and Lexical Environments in JavaScript. All JavaScript functions store a hidden reference to the lexical environment where they were created in. This info is stored in the [[Scopes]] property of the function.
Many different methods can be used to let JavaScript work with events: HTML event attributes can execute JavaScript code directly. HTML event attributes can call JavaScript functions. You can assign your own event handler functions to HTML elements.
Closures are important because they control what is and isn't in scope in a particular function, along with which variables are shared between sibling functions in the same containing scope.
I would put javascript files used by different widgets in different maps and load them statically (has got to be faster, plus the automatic browser caching if you have the right settings).
Then, dependently on how dynamic your framework is, I would make some kind of loader-javascript that automatically fetches and loads only those JS files that are needed for the widgets that are currently loaded on the page. All you would have to print in the script is perhaps some kind of require_js_file function, which will tell the loader which javascript does this widget use, and the loader would fetch it, but only if it has not already been loaded (for example if this is not the first run of the widget).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With