将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Can js delayed route matching / manually triggering the dispatch?
js是否延迟路由匹配/手动触发调度?

65 2

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?

时间: 原作者:

149 5

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
 })
 }
});


...