Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 909
user700284 Avatar asked May 02 '12 10:05

user700284


2 Answers

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/

like image 166
drinchev Avatar answered Nov 16 '22 03:11

drinchev


Works for me. Try to wrapper all the Backbone code in a documentReady block:

$(function(){
  // .. your backbone code
});​

Another thing is that maybe you have to change the order of the scripts loads:

  1. underscore
  2. jquery
  3. backbone
like image 26
fguillen Avatar answered Nov 16 '22 02:11

fguillen