How do I create multi-page applications with Meteor?

前端 未结 5 2039
醉话见心
醉话见心 2021-01-29 21:23

I am new to Javascript and just started fiddling around with Meteor out of curiosity. What really surprises me, is that it seems that all HTML content gets combined into a singl

5条回答
  •  抹茶落季
    2021-01-29 22:04

    I found the same problem. When the code gets bigger it is difficult to keep the code clean.

    Here goes my approach to this problem:

    I separate the different html pages as I would do with another web framework. There is an index.html where I store the root html page. And then for each big functional part I create a different template and place it in one different html. Meteor then merges them all. Finally I create a session variable called operation where I define what to show at each time.

    Here goes a simple example

    index.html

    
      My app name
    
    
    
     {{> splash}}
     {{> user}}
     {{> debates}}
    
    

    then in splash.html

    
    

    then in user.html

    
    

    and so on ...

    In the javascript code then I check when to print each template using the Session variable, like this:

    Template.splash.showSplash = function(){
        return Session.get("operation") == 'showSplash';
    }
    

    Finally the Backbone Router manages this Session variable

    var DebateRouter = Backbone.Router.extend({
    
      routes: {
        "": "showSplash",
        "user/:userId": "showUser",
        "showDebates": "showDebates",
        // ...
      },
      splash: function () {
       Session.set('operation', 'showSplash');
       this.navigate('/');
      },
      user: function (userId) {
       Session.set('operation', 'showUser');
       this.navigate('user/'+userId);
      },
      // etc...
    });
    

    I hope this pattern is helpful for other Meteor developers.

提交回复
热议问题