简体   繁体   English

如何在多个文件中组织视图

[英]How to organize views in multiple files

We are creating a mobile hybrid app using kendoui mobile framework and icenium. 我们正在使用kendoui移动框架和icenium创建一个移动混合应用程序。 I know it is a one page-app with lots of views in it. 我知道这是一个单页面应用程序,其中包含很多视图。 However if we add a lot of views index.html can get very big and hard to maintain. 但是,如果我们添加很多视图,index.html可能会变得很大且难以维护。 I wonder if there is a way to organize views into individual files and the include them into main page somehow. 我想知道是否有一种方法可以将视图组织到单个文件中,并以某种方式将其包含到主页中。 Something similar to partial views in asp.net. 与asp.net中的局部视图类似。 I cannot find a way of doing it, maybe there is some js library that does it? 我找不到做到这一点的方法,也许有一些js库可以做到这一点?

You don't need an external library to achieve this. 您不需要外部库即可实现此目的。 Kendo supports this out of the box using a feature called remote views . Kendo使用称为“ 远程视图”的功能开箱即用地支持此功能。 You can have main view in index.html and other views in other.html files. 您可以在index.html中拥有主视图,而在other.html文件中拥有其他视图。 See documentation here: http://docs.kendoui.com/getting-started/mobile/application#remote-views 请参阅此处的文档: http : //docs.kendoui.c​​om/getting-started/mobile/application#remote-views

Just add the file name (including path) in which your remote view is defined without # . 只需添加不使用 #定义远程视图的文件名(包括路径)。

RequireJS has a text plugin for this purpose. RequireJS为此有一个文本插件 With it, you can load html, templates, or other text files in the same way you would your Javascript dependencies. 使用它,您可以以与Java依赖项相同的方式加载html,模板或其他文本文件。 A contrived example: 一个人为的例子:

define([
  "lib/underscore",
  "lib/backbone",  
  "text!views/template.html"
], 

function (_, Backbone, template) {
  return Backbone.View.extend({

    template: _.template(template),

    initialize: function() {
      this.listenTo(this.model, "change", this.render);
    },

    render: function() {
      this.$el.html(this.template(this.model.toJSON()));
    }
  }); 
});

you can use RequireJS, i haven't use it but i know you can use, i have this code 您可以使用RequireJS,我还没有使用过,但是我知道您可以使用,我有这段代码

// The view Loader. Used to asynchronously load views located in separate .html files
    window.templateLoader = {

    load: function(views, callback) {

        var deferreds = [];

        $.each(views, function(index, view) {
            if (uknowLocate.Views[view]) {
                deferreds.push($.get('js/templates/' + view + '.php', function(data) {
                    uknowLocate.Views[view].prototype.template = _.template(data);
                }, 'html'));
            } else {
                console.error(view + " not found");
            }
        });

        $.when.apply(null, deferreds).done(callback);
    }

};

and i use it in this way: 我以这种方式使用它:

uknowLocate.init = function () {
    templateLoader.load(['HomeView', 'MainMenuView',
        'GeofencesNewView',
        'CheckinOnetimeView','CheckinScheduledView','CheckinNewView','CheckinRecurrentView',
        'LocationhistoryView'], function () {
        app = new uknowLocate.Routers.ApplicationRouter();
        Backbone.history.start({pushState:false, root:'/project/folder/'});
    });
};

And in this way i load my templates, this is for Backbone, you can take the idea 这样,我加载模板,这是针对Backbone的,您可以采用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM