Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding bootstrap from specific routes in Meteor

I was hoping anyone could give some input on this,

I'm creating a meteor app in which I would like to use bootstrap to creating the admin environment, but have the visitor facing side using custom css. When I add the bootstrap package to my app using meteor it's available on every page, is there a way to restrict the loading of bootstrap to routes that are in '/admin' ?

like image 538
Jasper2_0 Avatar asked Jul 16 '13 20:07

Jasper2_0


3 Answers

When you add bootstrap package it's not possible. You can, however, add bootstrap csses to public directory and then load them in a header subtemplate that will only be rendered when you're in the dashboard.

 


 

EDIT

But then how would you go about creating seperate head templates?

Easy:

<head>
    ...
    {{> adminHeader}}
    ...
</head>


<template name="adminHeader">
    {{#if adminPage}}
        ... // Put links to bootstrap here
    {{/if}}
</template>



Template.adminHeader.adminPage = function() {
    return Session.get('adminPage');
}


Meteor.router.add({
    '/admin': function() {
        Session.set('adminPage', true);
        ...
    }
});
like image 92
Hubert OG Avatar answered Oct 18 '22 03:10

Hubert OG


DISCLAIMER: I am unsure of a 'meteor way' to do this, so here is how I would do it with plain JS.

jQuery

$("link[href='bootstrap.css']").remove();

JS - Credit to javascriptkit

function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}

removejscssfile("bootstrap.css", "css")

However, doing that would complete remove it from the page. I am not sure whether meteor would then try to readd it when a user goes to another page. If that does not automatically get readded, then you have an issue of bootstrap not being included when someone goes from the admin section to the main site, which would break the look of the site.

The way I would get around that would be to disable and enable the stylesheets:

Meteor.autorun(function(){
  if(Session.get('nobootstrap')){
    $("link[href='bootstrap.css']").disabled = true;
  }else{
    $("link[href='bootstrap.css']").disabled = false;
  }
});

There my be other bootstrap resources which may need to be removed, take a look at what your page is loading.

To use jQuery in the same way but for the javascript files, remember to change link to script and href to src


From my tests, Meteor does not automatically re-add the files once they have been removed so you would need to find some way of re-adding them dynamically if you want the same user to be able to go back and forth between the main site and the admin site. Or simply if the http referrer to the main site is from the admin, force reload the page and then the bootstrap resources will load and everything will look pretty.

P.s. make sure you get the href correct for the jQuery version

like image 23
1321941 Avatar answered Oct 18 '22 02:10

1321941


If somebody is interested in including any js/css files, I've written a helper for it:

if (Meteor.isClient) {

  // dynamic js / css include helper from public folder
  Handlebars.registerHelper("INCLUDE_FILES", function(files) {
    if (files != undefined) {
      var array = files.split(',');
      array.forEach(function(entity){
        var regex = /(?:\.([^.]+))?$/;
        var extension = regex.exec(entity)[1];
        if(extension == "js"){
          $('head').append('<script src="' + entity + '" data-dynamicJsCss type="text/javascript" ></script>');
        } else if (extension == "css"){
          $('head').append('<link href="' + entity + '" data-dynamicJsCss type="text/css" rel="stylesheet" />');
        };
      });
    }
  });
  Router.onStop(function(){
    $("[data-dynamicJsCss]").remove();
  });

}

Then simply use:

{{INCLUDE_FILES '/css/html5reset.css, /js/test.js'}}

in any of your loaded templates :)

like image 1
Dariusz Sikorski Avatar answered Oct 18 '22 04:10

Dariusz Sikorski