如何理解所需的布局已经创建

How to understand that required layout has already been created?

本文关键字:创建 布局 何理解      更新时间:2023-09-26

在我的应用程序中,我有一些具有相似设计的页面和一个设计完全不同的主页。因此,我的问题是如何理解所需的布局(对于具有类似设计的页面)已经创建并且不重新渲染它。顺便说一句,在进一步的开发中,我可能会添加一些具有不同设计的新页面。我想过一些解决方案,但它们对我来说似乎都是反模式。所以,我需要以正确的方式组织我的木偶控制器。现在它看起来像这样:

define(['backbone', 'marionette', 'app/app', 'session'], function (Backbone, Marionette, app, session) {
'use strict';
var main = app.mainRegion;
function mainContent (view, model, options) {
    require([view, model], function (View, Model) {
        if (...) { //if main.currentView is already 'layout/main' then...
            var model = new Model(options);
            main.currentView.content.show(new View({ model: model }));
        } else {
            require(['layout/main'], function(Layout) {
                main.show(new Layout());
                var model = new Model(options);
                main.currentView.content.show(new View({ model: model }));
            })
        }
    });
}
return Marionette.Controller.extend({
    home: function () {
        if (session.getToken() === '') {
            require(['layout/home', 'login/view', 'register/view', 'login/model',
            'register/model'], function(Layout, LoginView, RegisterView, LoginModel, RegisterModel) {
                main.show(new Layout());
                main.currentView.login.show(new LoginView({ model: new LoginModel() }));
                main.currentView.register.show(new RegisterView({ model: new RegisterModel() }));
            });
        } else {
            Backbone.history.navigate('/feed', { trigger: true });
        }
    },
    feed: function () {
        mainContent('feed/view', 'feed/model', {});
    },
    profile: function(username) {
        mainContent('profile/view', 'profile/model', { username: username });
    }
});

});

如果您使用的是区域,则无需担心。仅当视图与区域中的当前视图不同时,才会重新呈现视图。另外,您可以手动检查:

if (main.currentView instanceof Layout) {
    ...
}