Miaucl
Miaucl

Reputation: 364

Backbone Underscore Version Error

I just started learning how to make a single view application with backbone (and underscore). I read a tutorial but when I tried to copy the most basic Version of a backbone App and put it on my server, i got an Error. The code was okay but after a while, a figured out, that my backbone.js and underscore.js files do not work together. So I downloaded the newest versions from their websites: Still not working... What am I doing wrong?

Thank you for your answers.

Here my code: index.html

<!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
  </head>
  <body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>

    <script src="js/1.js" type="text/javascript"></script>
  </body>
  </html>

and: js/1.js

$(function() {
    var ListView = Backbone.View.extend({
        el: $('body'), // attaches `this.el` to an existing element.

        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(){
            $(this.el).append("<ul> <li>hello world</li> </ul>");
        }
    });

    var listView = new ListView();
});

The JQuery and JSon2 are working fine. The backbone and underscore are here forked, but I also tried with the newest versions.

EDIT:

I tried now the two lines from below and the newest version of JQuery:

<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>

but I still get this error:

[Error] TypeError: undefined is not a valid argument for 'instanceof' (evaluating 't instanceof a.$')
    setElement (backbone-min.js, line 1)
    _ensureElement (backbone-min.js, line 1)
    View (backbone-min.js, line 1)
    r (backbone-min.js, line 1)
    (anonyme Funktion) (1.js, line 14)
    fire (jquery-1.11.2.js, line 3143)
    fireWith (jquery-1.11.2.js, line 3255)
    ready (jquery-1.11.2.js, line 3467)
    completed (jquery-1.11.2.js, line 3498)

SOLVED:

Thanks guys. I just found in a newer tut a combination, which is working fine:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>

Upvotes: 0

Views: 447

Answers (1)

ChinKang
ChinKang

Reputation: 4342

 <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>

simply your underscore version is too old, use the version 1.7.0. The backbone version u're using trying to use _.has which is only introduced in underscore version 1.3.1

Upvotes: 1

Related Questions