13

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});
});
squeezemylime
  • 2,102
  • 3
  • 18
  • 26
  • I suspect this has to do with load speed/order and that when logged in, loading takes longer, leading to appRouter being ready on time. Try putting the appRouter declaration inside Meteor.startup. – Rahul Jan 26 '13 at 23:24
  • tried this Rahul - didn't work – squeezemylime Jan 27 '13 at 13:49
  • I haven't worked on Win8 Metro Apps yet,so I don't know if there is a development console available like in Browsers. If there is, I'd invoke the `appRouter = new Aphorism;` just before `Backbone.history.start({pushState: true});` and assign the appRoute to a global variable to see if it is initialised at all. – wowpatrick Feb 01 '13 at 13:43

3 Answers3

1

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.

philfreo
  • 41,941
  • 26
  • 128
  • 141
1

have you tried waiting for the DOM to be ready? eg: $( Backbone.history.start )

p3drosola
  • 5,784
  • 2
  • 23
  • 30
1

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.

benaiah
  • 21
  • 1