I’m trying to learn how to use backbone.js. Problem is that a tutorial I’m following (the Railscast) is using coffeescript, and I’d rather use jquery. I know how to change a function in coffeescript to jquery, but I didn’t know what to change about this definition of a route
routes:
'': 'index'
Am I supposed to put brackets around it? The alert’s not working, so I’m assuming something’s wrong with the definition of the route.
This was the original coffeescript
Backboner.Routers.Entries = Backbone.Router.extend
routes:
'': 'index'
index: ->
alert "homepage"
javascript/jquery
Backboner.Routers.Entries = Backbone.Router.extend({
routes:
'': 'index'
index: function(){
alert("homepage");
}
});
Update: I made the changes that were suggested but the alert’s not appearing.
Javascripts/Backboner.js
window.Backboner = {
Models: {},
Collections: {},
Views: {},
Routers: {},
init: function() {
new Backboner.Routers.Entries();
Backbone.history.start();
}
};
Javascripts/routers/entries.js
Backboner.Routers.Entries = Backbone.Router.extend({
routes: {
'': 'index'
},
index: function(){
return alert('homepage');
}
});
Second update:
Putting document ready into file
window.Backboner = {
Models: {},
Collections: {},
Views: {},
Routers: {},
init: function() {
new Backboner.Routers.Entries();
Backbone.history.start();
}
};
$(document).ready(function() {
return Backboner.init();
});
third update
the manifest file
//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require backboner
//= require_tree ../templates/
//= require_tree .//models
//= require_tree .//collections
//= require_tree .//views
//= require_tree .//routers
//= require_tree .
The short answer is yes, you are supposed to put curly braces around it. In the future, you can convert from CoffeeScript to Javascript right on CoffeeScript.org by clicking “Try CoffeeScript”. Paste in the CoffeeScript and you will see the Javascript output.
Keep in mind, CoffeeScript has significant white space, so your original question was incorrectly formatted. It should have been:
which using the CoffeeScript website gives: