How to synchronise/organise modules using requirejs and Backbone.Marionette

后端 未结 1 1038
小蘑菇
小蘑菇 2021-02-11 06:04

I organised the file structure of my web app, which is using RequireJs and Backbone.Marionette,in this way:

|- main.js
|- app.js
|- /subapp1
    |- subapp1.js
           


        
相关标签:
1条回答
  • 2021-02-11 06:39

    1) Is right to load asynchronously the app and subapps even if subapps need app?

    You should be able to asynchronously load your app and any of the sub-apps. If the sub-apps require the main app, they should list it as a dependency. RequireJS will resolve the dependencies for you.

    Only thing you should watch out for are circular dependencies, but that shouldn't be anything you can't fix.

    2) for the subApps is right to load the router which needs the app?

    Listing the routers of your sub-apps in your main.js sounds right to me.

    3) Since the subApps could need some models I decided to load it in app.js. Is it right this way?

    A module should list any and all other modules it uses as its dependencies. Therefore, if app.js and subapp1/subapp1.js both require models/user.js, they should both list models/user.js as one of their dependencies.

    This way, if you change any of the modules in such a way that it no longer requires models/user.js you can remove the dependency from that particular module without risk of missing dependencies in another module.

    Besides being the savest way of managing your dependencies, it's also nice and clear for other developers or even yourself when looking at your code in the future.

    4) about this module I am not sure about the app.addInitializer.

    I am not sure for example if the app.userModel will be fetched when I perform app.header.show. Should it be ok?

    Fetching data should be initiated manually. While showing a view in a Region does trigger the View to be rendered, it doesn't trigger the view's Model or Collection to fetch data.

    What you can do, is to let your view listen to the events on your Model or Collection and re-render the view once the Model or Collection has successfully fetched new data. Marionette's CollectionView automatically binds some events to add, change and remove events of it's Collection in orde to automatically manage it's ItemViews if the Collection changes in any way.

    5) is it ok to load from the main.js the subapp1/subapp1.router instead of subapp1/subapp1?

    As I already stated in question 2, it's good to load your routers in main.js.

    One reason to do this, is that you could decide not to load all your sub-apps up front, but instead only load them once they're actually needed. The moment such a sub-app's routes get triggered is as good a time as any to load that sub-app's core module and possibly some of it's dependencies, but that would of-course require the sub-app's Router to be activated before the sub-app is loaded.

    0 讨论(0)
提交回复
热议问题