Search code examples
javascriptbackbone.jsbackbone-routing

How can i run a callback function after Backbone.history.loadUrl?


I tried the below code that does not work. Also i searched via google but find nothing. Backbone's official documentation does not have an entry about loadUrl.

app_router.on('route:page', function(id, name) {
    ...
});

app_router.on('route:file', function(id, name) {
      // first open the page
      Backbone.history.loadUrl("page", function() {
        // and open the file manager after page is loaded
      });
});

Solution

  • You cannot pass callback in loadUrl.

    From annotated source

    Attempt to load the current URL fragment. 
    If a route succeeds with a match, returns true. 
    If no defined routes matches the fragment, returns false.
    

    And function definition

    loadUrl: function(fragment)
    

    Instead use

     router.on("route", function() {
       ...
     });
    

    To answer your edited question, you can do something like

     app_router.on('route:file', function(id, name) {
        // first open the page
        if(Backbone.history.loadUrl("page")){
           // and open the file manager after page is loaded
        });
     });