0

我有一个 Backbone.Marionette 应用程序,其模块在初始化时通过 socket.io 获取模型数据。我用这个启动路由器:

App.on('initialize:after', function(){
    Backbone.history.start();
});

问题是路由器在获取数据之前启动,所以我的视图没有呈现。

在页面加载时获取模型后,我应该怎么做才能显示我的视图?

4

2 回答 2

0

消除

Backbone.history.start();

从这里开始并将其放入 fetch 的成功回调中。

您始终可以在启动时初始化路由器,但实际上history.start()会进行重定向。

更新

检测多次获取成功:

//Declare an array 
App.modules = [];

//When you initialise each module, push it to array
App.modules.push(this.id);

//Declare a counter for modules 
App.moduleCounter = 0;

var startHistory = function() {
     App.moduleCounter++;        

     if(App.modules.length === App.moduleCounter) {
           Backbone.history.start();
      }    
 } 

//Call this function on each success 
 startHistory();
于 2013-10-21T13:04:12.973 回答
0

您的问题似乎与路由器无关。路由器只需解析 URL 以调用正确的操作。如果您的视图未呈现,则意味着您的应用程序未正确调用控制器操作。

您应该等待视图中的数据(无论路由器状态如何)。一种方便的方法是使用 jQuery 延迟。您可以在我的一些博客文章中了解它们:

另外,你可以在这里看到一个用法:https ://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/show/show_controller.js这段代码取自我的书在 Marionette 上,并使用延迟/承诺在“获取”数据时显示加载视图。

于 2013-10-21T14:06:35.037 回答