I can’t get the most basic functions of Backbone.js to work on my web application.
JQuery, JSON, and Underscore.js do work, but for some reason Backbone.js doesn’t.
I know this is a very simple problem, but scouring the Internet has not helped thus far.
I would really appreciate any help!
Details of app:
Framework: Rails 3.2.6
All JS files are stored under app/assets/javascripts
JS files present: index.js, application.js, backbone.js, json2.js, underscore.js
How I’m testing which libraries work
I’ve put tests to see what works in the index.js file, which contains the following:
$(function(){
//test that this Javascript file is working:
alert('index.js javascript file works');
//test that jQuery is working:
var jQuery_test = $("#app").attr('id');
alert("jQuery works? (following should be 'app'): " + jQuery_test);
//test that JSON is working
var json_obj = {"key1":"value1", "key2":"value2", "key3":"value3"};
var json_string = JSON.stringify(json_obj);
alert('JSON library works? (following should be a JSON string): ' + json_string);
//test that Underscore is working
var array1 = ["one", "two", "three"];
var x = _.first(array1);
alert("Underscore library works? (following should be 'one'): " + x);
//test that Backbone is working
var ItemView = Backbone.View.extend({
tagName: 'li'
});
var item = new ItemView();
alert("Backbone library works? (following should be '[Object HTMLLIElement]'): " + item.el);
});
The tests above pass for all but Backbone.
Perhaps I’m using a bad test?
Really have no idea what could be wrong.
I dont think it has to do with how I’m loading the files, because I’m loading Backbone the same way I’m loading the other libraries, and they all work.
Thanks for any help!
Underscore is a hard dependency for Backbone and should be loaded before backbone.js, as any other dependency you may have (jQuery/Zepto).