0

I'm doing a hello world example of a backbone app, and it only works if the script src tags are contained in the body, not in the header (where I'd expect them to be). If I have them in the header and do alert(el) it says undefined. However, if I have the script src tags in the body and do alert(el) it says object HTMLBodyElement and the hello world example works. I also note that the tutorial I'm looking at has the script src tags inside the body http://arturadib.com/hello-backbonejs/ (which is the only way I can get it to work)

Why is that?

Doesn't work

 <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
    <link rel="stylesheet" href="static/style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  <script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
  <script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
  <script src="static/backbone.localStorage-min.js"></script>
  <script src="app.js"></script>

  </head>
  <body>



  </body>
  </html>

Works

 <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
    <link rel="stylesheet" href="static/style.css">


  </head>
  <body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  <script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
  <script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
  <script src="static/backbone.localStorage-min.js"></script>
  <script src="app.js"></script>


  </body>
  </html>

This is the javascript

(function($){
  // **ListView class**: Our main app view.
  var ListView = Backbone.View.extend({    
    el: $('body'), // attaches `this.el` to an existing element.
    // `initialize()`: Automatically called upon instantiation. Where you make all types of bindings, _excluding_ UI events, such as clicks, etc.
    initialize: function(){
      _.bindAll(this, 'render'); // fixes loss of context for 'this' within methods

       this.render(); // not all views are self-rendering. This one is.

    },
    // `render()`: Function in charge of rendering the entire view in `this.el`. Needs to be manually called by the user.
    render: function(){
      $(this.el).append("<ul> <li>hello world</li> </ul>");
      alert(this.el);
    }
  });

  // **listView instance**: Instantiate main app view.
  var listView = new ListView();      
})(jQuery);
4

1 回答 1

2

当你这样做时:

<body>
   <script src="..."></script>
   ...
</body>

<body>加载脚本时,您的 DOM中将有一个。<body>特别是,当你点击这个时,你会有一个:

el: $('body')

当你这样做时:

<head>
    <script src="..."></script>
</head>
<body>
    ...
</body>

<body>当你击中时不会有一个,el: $('body')所以你elundefined在 Backbone unwraps 之后结束$('body')

我认为您对以下之间的区别感到困惑:

(function($) { /* ... */ })(jQuery);

$(function() { /* ... */ });

第一个立即执行函数,第二个同

$(document).ready(function() { /* ... */ });

并且在 DOM 准备好时执行该函数。您可能希望您的 JavaScript 看起来更像这样:

$(function() {
  var ListView = Backbone.View.extend({ /* ... */ });
  var listView = new ListView();
  // And now do something with listView
});
于 2012-09-23T01:43:34.883 回答