Reputation: 193
It looks like other people have had a similar problem when trying to create a Backbone View. But I think my problem is different. Here is how I am including backbone in my page.
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript">
//<!--
jQuery.noConflict ();
//-->
</script>
<!--[if lt IE 9]><script type="text/javascript" src="/js/html5.js"></script><![endif]-->
<script type="text/javascript" src="/js/backbone-min.js"></script>
<script type="text/javascript" src="/js/showbase.js"></script>
I added the call to jQuery.noConflict() because I thought that might be the problem. It wasn't.
Here is what I have in my showbase.js file so far. This simple test fails.
jQuery (document).ready (
function ()
{
var props = '';
for (var p in Backbone)
props += p + ': ' + typeof Backbone [p] + "\n";
alert (props);
props = '';
for (var p in Backbone.Model)
props += p + ': ' + typeof Backbone.Model [p] + "\n";
alert (props);
alert (typeof Backbone.Model);
}
);
In the first alert, I get:
VERSION: string
$: function
noConflict: function
emulateHTTP: boolean
emulateJSON: boolean
Events: object
In the second alert, I get nothing at all.
In the third alert, I get "undefined".
The error message I get from Firebug says "TypeError: h is undefined" and it points me deep within the backbone-min.js file somewhere.
I am following the Todos example at http://backbonejs.org/docs/todos.html. In that example, the first thing they do is extend the Backbone.Model class in the jQuery document ready function. That is all I am doing. But the Backbone.Model class doesn't even exist at that point.
I am not trying to access this.Model. I never reach a point in my code where I have access to a this. The Model class is simply undefined within the Backbone object. Do I need to include something other than the backbone-min.js file?
Upvotes: 3
Views: 2607
Reputation: 193
The problem was that underscore.js was not included. It is required by backbone.js, as Loamhoof pointed out.
I did some further research. According to this page, backbone only depends on json2.js to provide the JSON.parse and JSON.stringify functions, which are now part of plain javascript in most browsers. So according to the comments on that page, json2.js should only be needed to support older browsers that do not provide JSON functionality.
Upvotes: 7