RequireJS with basic dependencies

I am building a website using requireJS.

this is the first time i use requireJS.

i configured requireJS with paths in main.js:

require.config({ paths: { 'jquery': 'libs/jquery/1.7.2/jquery', 'underscore': 'libs/underscore/1.3.3-amdjs/underscore', // AMD support 'backbone': 'libs/backbone/0.9.2-amdjs/backbone', // AMD support 'marionette': 'libs/marionette/0.9.3-amd/backbone.marionette', // AMD support 'templates': '../templates' } }); 

and in the models, presentations, collections that I use;

 define([ 'jquery', 'backbone' ], function ($) { var Geo = Backbone.Model.extend({}); return Geo; }); 

which I don’t understand: why should I continue to define jquery and the spine as a dependency. I mean this project, and models will never work without jquery / backbone.

so why not add jquery and basebode to index.html as script tags and keep links to them in each object. I understand that it will pollute the global namespace, but is it not so reasonable?

any recommendations.

amuses

+4
source share
4 answers

@Guy, it’s not necessary to define them, but if you want to be consistent, it is better than you. This is the architecture Require.JS offers. This does not mean that it will try to load jQuery or Backbone every time you define them as deps.

Also, in your example, you don't need to use $ in function() callback arguments. jQuery and $ will already be in the window.

 define([ 'jquery', 'backbone' ], function ($) { var Geo = Backbone.Model.extend({}); return Geo; }); 

In addition, in production, since we sometimes use a lot of dependencies, we do it this way: every time you specify require in the dependencies, and then explicitly assign vars, because some dependencies do not support AMD and do not return what you expect:

 define([ 'require' 'moduleA', 'moduleB' ], function (require) { var moduleA = require('moduleA'), moduleB = require('moduleB'); /* code */ return; }); 
+2
source

I think your understanding is correct, since jQuery is everywhere, will be used as a global link on the index.html page completely in my project, I am doing this.

0
source

I think it is very useful to add all js files to RequireJS if you use r.js to optimize your project.

0
source

If you create modules A and B and want to drop them into another project, dependencies are explicitly declared. What if another project just uses ExtJS without jQuery? Or what if they use zepto and you need a jquery library? Just adding a configuration for jquery is enough .. and jquery will load if necessary.

You cannot rely on the existence of shared libraries. Wrapping a module in the requirejs shell ensures that library dependencies exist. And it's a little faster :) http://jsperf.com/requirejs-include-faster (well, obviously, except that ... go).

Also, if you are not actually using jquery, you do not need to require it. Why are you using in the base model? Even in the view, you can use this. $ El, which returns a jquery object to search for - this. $ El.find ('. Someclass')

0
source

All Articles