Answer the question
In order to leave comments, you need to log in
How to organize javascript code?
Actually a subject. I develop on Symfony 1/2, all js code is a noodles from jQuery calls, its plugins and my crutches stuffed into separate files, for example: profile.js in /profile, post.js in /post, etc. I can’t come to any understanding of how to organize all this code, it is also possible to assemble it with something (and whether it is necessary).
I started to google on this topic, I come across articles about backbone / angular / etc., but for some reason it seems to me to be something superfluous, so far I don’t need templating on the client as such, routes again on #! some terrible ones. In general, I ask you to help me figure it out, because my head is a complete mess.
Answer the question
In order to leave comments, you need to log in
It seems to me that you need to do this:
1. write a microkernel (which will contain, in particular, the mentioned RequireJS and the specific architecture of the project).
2. write a macrokernel (in which there will be all the entire js code) in order to avoid all sorts of miraculous features in the middle of the HTTP response. This is done by a wonderful thing called GruntJS and a set of per-URL scripts specified by the author.
By default, a lightweight microkernel is always loaded; if something needs to be loaded, it pulls the macrokernel (gradually in the background) - which, due to modularity, is gradually cached.
AMD is a little off the mark, I think...
If the code is decomposed into a tree according to dependencies: the common code is at the root, the private ones are in subdirectories, then you can store it that way. If you want to keep it in one directory, then either the subdirectory tree is duplicated there, or everything is in one heap, and pulling out from it is organized through dependencies. If a private code or plugin is used in more than one place, it is moved to a common directory root. Dependencies can be visualized in a single JSON file so that the script can reach without manually editing the codes in many places. A similar question often arises in medium-complex systems with more than 2 dozen files. On the topic, rather, it is better to google on @require JS", "commonJS", AMD, although they do not resolve the issue of file paths - this is all at the discretion of the programmer.
For the author, it may no longer be relevant, but for those who google the topic, I recently wrote an article about this habrahabr.ru/post/218485
A simple approach to structuring code on the client
Get all the code out of your head and decompose the problem as a whole. And it will immediately become clear where what code to “put”. Well, or maybe even decompose the code too.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question