0

In my Onsen app I have the following splitter. I am using Jade, and rendering all the other pages from the list items in html (despite the fact that they are in separate jade files) by including the files at the bottom of the page, as shown below:

body(ng-controller='...')
    ons-splitter(var='mySplitter')
      ons-splitter-side(var='menu' side='left' width='220px' collapse swipeable)
        ons-page
          ons-list
            ons-list-item(ng-click="root.load('home.jade')", tappable='')
            | Home
          ons-list-item(ng-click="root.load('search.jade')", tappable='')
            | Search
            ... more list items

    ons-template(id='home.jade')
      ons-page(ng-controller='...')
        ons-toolbar
        .left
          ons-toolbar-button(ng-click='mySplitter.left.open()')
            ons-icon(icon='md-menu')
        .center
          | My App

        //- google maps stuff
        ons-input#pac-input.controls(type='text', placeholder='Search Box')
        div#map.col-md-12

        ons-bottom-toolbar
          .center
          | MyApp

    include search.jade

I believe this is a dirty shortcut, and will load the contents of search.jade (as well as every other file I include) before the user even clicks the item in the splitter.

I do not want this functionality. I would like to instead have server code in NodeJs that renders the jade files in html when they are ready to be displayed to the user. Something like this:

jade.renderFile('search.jade')

This angular code is currently how I am loading the page from the item in the splitter:

mySplitter.content.load(page)
  .then(function() {
  $scope.pop = page;
  mySplitter.left.close();
});

However I am very confused about how to write this in a node route. Do I just abandon the splitter function in angular?

Can anyone help clarify this for me and show me a clear example of how to write the node route to render the jade files as html each time they are loaded?

Please see solution 1 of the selected answer from this stack overflow post for a reference of what exactly I am trying to do: stack overflow post

I am currently using solution 2 from that post.

I believe this is the relevant code in server.js:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

However when I change jade to html, I receive the error: Error: Cannot find module 'html'

All my front-end files in the views folder have .jade extensions and are written in jade.

Update Here is how I am serving index.jade (which is in the views folder) in a file called index.js:

module.exports = function(app){

  /* Get home page. */
  app.get('/', function(req, res, next) {
    res.render('index', { title: 'My App' });
  });
}

This was my old route NodeJS route which is no longer being used because of the splitter:

    // get user search page
    app.get('/user/search', function(req, res, next) {
        return res.render('searchForTrainer');
    });
Community
  • 1
  • 1
rgins16
  • 349
  • 1
  • 3
  • 11
  • Since here you are asking about the server you should share your sever code instead of the pages. You probably have something like `server.js` - so all the changes should be made there. You don't need to touch the splitter - if the server serves html (the output of jade.renderFile) then the splitter will handle that without a problem. So you shouldn't change anything related to angular or the splitter - only the server code. Share the server code since the solution depends on what you are using over there. :) – Ilia Yatchev Jul 10 '16 at 02:39
  • Thank you for clarifying. I apologize, this is obviously my first NodeJs/Onsen app, and I am new to web programming in general. I have added what I believe to be the relevant server code from Server.js. – rgins16 Jul 10 '16 at 14:36

1 Answers1

1

Hmm. Since your code seems relatively small I would guess that what it does may be just serving all your files from views and actually "rendering" them. So probably you are just failing to access them properly later on. Maybe you have a url like /search.html or /search (instead of /search.jade). Could you try to confirm whether you can access such a url?

Also is your index.jade file served in some other way like startingPoint: 'index.jade' or something similar or is it also located in the views folder?

Basically as long as your index file has the same treatment as your other views then everything should be fine.

Update: With what you just provided we can see the way in which you are serving your index.

app.get('/', function(req, res, next) {
    res.render('index', { title: 'Fitness App' });
});

The equivalent of that is exactly the same as what you said you had before:

app.get('/user/search', function(req, res, next) {
    return res.render('searchForTrainer');
});

Here res.render is what converts your jade into html and then returns it to the client. Since the splitter is expecting html that means you shouldn't have made changes to the server when you started using it.

Here is how the process looks like:

       Client          |        HTTP         |      Server
                       |                     |
 content.load('page')  →     GET /page       ↘ 
                       |                     | res.render('page') // convert jade to html
   html is loaded      ← 200 OK html content ↙
 in splitter.content   |                     |

TL;DR - if you use your old route everything should be fine. Just remember to change the page url in the splitter from search.jade to /user/search (or whatever the url for will be).

Ilia Yatchev
  • 1,254
  • 7
  • 9
  • I'm not sure what you mean by the urls because I am now using the splitter to load the pages instead of node routes. Additionally, please see my update above as to how I am serving index.jade and where it is located. – rgins16 Jul 10 '16 at 20:45
  • I am confused because it seems to me that index.jade has the same treatment as all my other views. Yet, the other views are not rendering. – rgins16 Jul 10 '16 at 20:45
  • I updated the answer to match your updated question. Basically what you had before is what you should have now - the use of the splitter shouldn't change your server :) – Ilia Yatchev Jul 11 '16 at 05:30
  • wow... I am stunned. I can not believe I allowed myself to make such a stupid mistake. Thank you so much for your patience. It's all simple now haha. – rgins16 Jul 11 '16 at 12:55
  • Additionally I had to remove the template tag from the top of search.jade because it didn't need an id anymore. So for anyone who finds this post, the file starts with the page tag. – rgins16 Jul 11 '16 at 12:56
  • oh yeah - sorry I forgot to mention that. Anyway I'm glad you solved the issue. Only solution 3 left if you're interested :D. The one described here is the best one, so I guess good luck with the rest of your fitness app. Feel free to ask again if you have any questions. – Ilia Yatchev Jul 11 '16 at 18:18