1

我有一个这样的控件设置:

  1. 渲染一个异步加载的视图。
  2. 视图的数据也是异步加载的。
  3. 监听路由变化。其中一个路由处理程序显示一个模式,其中包含在步骤 2 中加载的模型的详细信息。

问题是用户可能会到达一个页面,该页面有一个指向模型的路由,该路由在控件初始化时不可用,因此显然没有加载模式。

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

如何在视图渲染后再次触发 distcher 或让控制器遍历路由?

4

1 回答 1

0

如果您将 findAll 返回的 Deferred 存储在某处,则可以在您的路线中使用它来判断它何时加载。

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

这是一个将页面存储在控件上的this.pages中的示例:(
看起来不太好但易于理解)

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
        })
    }
});
于 2014-02-03T02:07:43.043 回答