Reputation: 2102
In my Meteor app Backbone routers are only working when the user is logged in (via the accounts-base package). It's strange. This router itself works fine. The showSaying()
function isn't being called at all when the user is not logged in.
Below is the code in client.js
within the client folder. Do I need to do something with sessions or auto-publishing?
AphorismView = Backbone.View.extend({
el: "#aphorism-item",
initialize: function(){
_.bindAll(this, "render");
this.render();
},
render: function() {
this.$el.append("<p style='height:600px; background-color:blue;'>hi</p>");
}
});
// Creates a route to view the selected aphorism
var Aphorism = Backbone.Router.extend({
routes: {
"saying/:id": "showSaying"
},
showSaying: function (id) {
var aphorism_view = new AphorismView();
alert('Saying id ' + id + '.');
}
});
//establishes the router
appRouter = new Aphorism;
//Sets up backbone
Meteor.startup(function () {
filepicker.setKey("AerIOvsmAQRGaNdEv0judz");
filepicker.constructWidget(document.getElementById('attachment'));
Backbone.history.start({pushState: true});
});
Upvotes: 13
Views: 893
Reputation: 21
Try pulling the Backbone history declaration outside of Meteor.start
. There's no reason for it to be there. Also, I believe that showSaying
will only be called if you go to a route that has the id
parameter (i.e., you can't just go to http://app/saying/
).
In addition, you might need to manually call approuter.navigate
in order for the router to work if you're trying to navigate programmatically. (I don't know if you are, but it could be the problem).
Other than that, I can't find any issues with this specific piece of code. The problem might be somewhere else, if nothing I suggested solves it.
For an example of a working router implementation, you could look at https://github.com/Benaiah/Athenaeum/blob/master/client/router.coffee (disclaimer: it's from an app I'm writing) and try to find any other differences.
Hope that helps.
Upvotes: 1
Reputation: 5877
have you tried waiting for the DOM to be ready? eg: $( Backbone.history.start )
Upvotes: 1
Reputation: 43794
Your issue doesn't seem like a Backbone.js issue at all.
Have you tried putting a console.log statement inside the startup()
callback to verify that it's actually being called in all cases? If not, then that's between you and Meteor.
Upvotes: 1