简体   繁体   中英

backbone.js why scripts needs to be loaded in all js files

I am new to backbone.js . So i need the following questions answered to understand the structure to work with it..

What my understanding is that once the basic template has been loaded then we need to play with the views/js file only to get the other html..

1.But once i load jquery from my main.js file using require why do i need to define it in the view again.. 2.Also if in my views js file if i need to include a new js file then that is making request to the server right?

For ex:

   define([
     'jquery',
     'underscore',
     'backbone',
     'text!templates/list1.html',

     ], function( $, _, Backbone,  index, Constants ) {
     ....
     ....
     });

Below is my code structure

     js
     |-- collections
     |   |-- camp.js
     |
     |-- constants.js
     |-- data.json
     |-- main.js
     |-- models
     |   |-- camp.js
     |-- routers
     |   `-- router.js
     |-- templates
     |   |-- camp.html
     |-- vendor
     |   |-- backbone
     |   |   |-- backbone.js
     |   |   `-- backbone.localStorage.js
     |   |-- bootstrap.js
     |   |-- bootstrap.min.js
     |   |-- codemirror
     |   |   |-- addon
     |   |   |   |-- closebrackets.js
     |   |   |   |-- dialog.js
     |   |   |   |-- matchbrackets.js
     |   |   |   |-- match-highlighter.js
     |   |   |   |-- searchcursor.js
     |   |   |   `-- search.js
     |   |   |-- codemirror.css
     |   |   |-- codemirror.js
     |   |   |-- hint
     |   |   |   |-- javascript-hint.js
     |   |   |   |-- show-hint.css
     |   |   |   `-- show-hint.js
     |   |   |-- mode
     |   |   |   `-- javascript.js
     |   |   `-- themes
     |   |       `-- solarized.css
     |   |-- jquery
     |   |   `-- jquery.min.js
     |   |-- jquery-1.9.1.min.js
     |   |-- jquery-ui-1.10.0.custom.min.js
     |   |-- jquery-ui-1.10.1.custom.css
     |   |-- jqueryuicustom.min.js
     |   |-- modernizr-2.6.2-respond-1.1.0.min.js
     |   |-- require
     |   |   |-- require.js
     |   |   |-- require.min.js
     |   |   `-- text.js
     |   |-- require.js
     |   `-- underscore
     |       `-- lodash.min.js
     `-- views
         |-- camp.js
  1. RequireJS is designed to keep the global scope clean. You are defining modules with explicit dependencies. You need to define jQuery, Backbone, etc. as a dependency for each module you write. RequireJS will automatically figure out in which order they need to be fetched.

  2. Yes, there will be a round trip, but it is managed by RequireJS and it will happen before the code of your module (view) is executed. If you're worrying about the number of files fetched, take a look at the documentation [1].

[1] http://www.requirejs.org/docs/optimization.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM