Is it correct to map classes to site pages like this?

  • 0
    Hello, can you please tell me if it is correct to organize the code in this way?
    There is such content in app.js:
    import Route from "./route";
    import HomeController from "./controllers/HomeController";
    import BlogController from "./controllers/BlogController";
    Route.url(/\//, HomeController);
    Route.url(/blog/, BlogController);

    Simply put, the HomeController class is responsible for scripts on the home page.
    and for the scripts in the blog BlogController

    I do this for this reason:
    I don’t want to do a bunch of checks , that is, for example, there is no pagination and no filters on the main page, but the blog has pagination and filters, if the initialization of pagination and filters will be in one app.js, then you need to do a check are there any pagination and filter elements in the DOM, because if there is no validation, there will be an error on the main page.

    But there are also some doubts about the correctness of my decision, for example, if there are 10 different pages, and for them different scripts, then 9 calls to the url function will occur in app.js until we reach the desired one. But I like this code for its isolation, as everything is clearly separated, HomeController - everything for the main one, BlogController - everything for the blog.

    The thought is really spinning in my head, create a file:
    - blog.js - for the blog
    - home.js - for the main one
    But I am doing a project under laravel, and here is laravel-mix, it seems to me it will not be very correct to create separate files manually and then connect them in the view.

    In general, is this approach correct? Or maybe I'm making some kind of bike, thanks in advance for your answer.
    JavaScript Anonymous, Nov 24, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!