0

Im new to backbone and there is one problem I cant figure out. I use backbone with require.js. When I want to render some view for the first time (when I reload page in browser) index.html shows up immediately but then it is hidden for about 2 seconds and then showed again with rendered templates. If I then navigate between views (without hitting refresh) it works smooth, but that initial render does something wrong. Here is my code:

View:

define(['jquery','underscore','backbone', 'text!../../../templates/home/HomeView.tpl'], 
function($, _, Backbone, HomeTemplate)
{
var HomeView = Backbone.View.extend(
{
    events:{
    },

    render: function () 
    {
        var template = _.template(HomeTemplate);
        this.$el.html(template);

        return this;
    }

});

return HomeView;
});

Router:

define(['jquery','underscore','backbone'], 
function($, _, Backbone)
{ 
var container = $("#container");

var AppRouter = Backbone.Router.extend(
{                       
    routes: 
    {
        "": "home"
    },

    home: function()
    {
        require(['views/home/HomeView'], 
        function(HomeView)
        {
            var Home = new HomeView({el: container});
            Home.render();
        });
    }


}); 


var initialize = function()
{
    var app_router = new AppRouter();


    Backbone.history.start();   
};

return { initialize: initialize };

});

I get no error in console. Is this behavior normal?

4

1 回答 1

1

这是正常的。

RquireJs 通过 http 请求加载依赖项,这些请求确实有超时。此外,在浏览器中,最大并行下载量有限制。这些延迟在启动时很容易加起来长达 2 秒,再加上网络延迟。

如果您想最小化这种延迟,请使用 r.js 最小化文件,如RequireJS docs中所述。

编辑:如果缩小不能解决问题,浏览器分析工具可以帮助你。

于 2013-09-04T13:06:38.490 回答