Error while creating a backbone.js view - Expecting a function in instanceof check, but got [object Object]

user700284 picture user700284 · May 2, 2012 · Viewed 11.7k times · Source

I am trying to create a simple backbone.js view with the following code:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
        <!--<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script> -->
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var BodyView = Backbone.View.extend({
                el:$('body'),
                initialize : function() {
                    console.log("view init");
                }
            });

            new BodyView();
        </script>
    </body>
</html>

But

I am getting an error Expecting a function in instanceof check, but got [object Object]

on this line - new BodyView();

This code is working if i use an older version of backbone.js(0.3.3)

Any idea why this error is happening and how to solve it?.Thanks in advance.

P.S - I have been mainly following http://backbonetutorials.com/what-is-a-view/ and http://documentcloud.github.com/backbone/#View for creating this sample backbone js app.

Answer

drinchev picture drinchev · May 2, 2012

Basic things you should check out :

Wrap your new BodyView() in jQuery.ready like this :

 var BodyView = Backbone.View.extend({
     el:$('body'),
     initialize : function() {
         console.log("view init");
     }
 });

 $(function() {
        new BodyView();
 });

Load jQuery before Underscore and Backbone

Your script line for jquery should be before that of Backbone.

Your code actually works : http://jsfiddle.net/vTuPJ/