Can js delayed route matching / manually triggering the dispatch?

137 views Asked by At

I have a control set up like this:

  1. Renders a view loaded async.
  2. Data for the view is also loaded asynchronously.
  3. Listens to route changes. One of the route handler displays a modal with the details of a model loaded in step 2.

The problem is that the user might get to a page that has a route that point to a model, that is not available at the moment the control is initialized so, obviously, the modal is not loaded.

can.Control({
    init: function() {
       can.view('file.ejs', {pages: app.Model.Page.findAll()}, function(frag){
          // inject the fragment into DOM
       });
    },
    'page/:id/comments route': function() {
       // find the page in the list of models loaded, than display the modal
    }
});

How to I trigger the distcher again or make the controller go over the routes after the view is rendered?

1

There are 1 answers

0
complistic On

If you store the Deferred that findAll returns somewhere, you can use that in your route to tell when its loaded.

http://canjs.com/docs/can.when.html

Here is a example that stores the pages in the this.pages on the Control:
(not very nice to look at but easy to understand)

can.Control({
    init: function() {
        this.pages = app.Model.Page.findAll()
        can.view('file.ejs', {pages: pages}, function(frag){
            // inject the fragment into DOM
        });
    },
    'page/:id/comments route': function() {
        can.when(this.pages).then(function(pages){
            // find the page in the list of models loaded, than display the modal
        })
    }
});