0

From what I understand, one benefit of using AMD managing dependencies that get loaded, But in single-page application using require.js all of the dependencies will be loaded.

For example using backbone or something similar, lets say I have the following files

main.js requires router.js
router.js requires view_a.js and view_b.js

router.js renders the views

routes: {  
   "view_b":"b",   
   "view_a":"a"  
},  
a:function{  
  showView(new ViewA());  
},   
b:function{  
  showView(new ViewB());  
}

Now lets say I go to example.com/#view_b which only requires the code in view_b.js but doesn't use anything from view_a.js. Is there anyway to only loaded or prioritize the loading view_b.js using require.js or something similar?

null_radix
  • 692
  • 2
  • 6
  • 19

1 Answers1

0

You can't because require.js need all dependencies of your file to be loaded before executing it.

I see two solutions to your problem:

  • Create two distinct Backbone.Router, like that you only "require" needed modules for each router (IMHO it's not the best solution)

  • Use require.js optimizer, this will combine all your Javascript files into one. Of course you only use it for production, not in development.

julesbou
  • 5,570
  • 4
  • 31
  • 36