Julien Le Coupanec
Julien Le Coupanec

Reputation: 7996

Meteor - How to load a JS library for only specific users?

I'm designing an administration interface with graphics and so on and I'm using the Highcharts Library (140kb). I want this file to be loaded only for admin users. What would be the best way to do it?

I just saw that we could use Iron-Router to conditionally load JavaScript but I don't like the idea to handle this kind of things inside the router as below:

Router.map ->
  @route 'admin',
    path: '/admin'
    template: 'admin'
    action: ->
      $.getScript '/js/moment.min.js', (data, textStatus, jqxhr) ->
        if jqxhr.status is 200
          @render()

NOTE: I wrote a little blog post to load a library for only specific users with Meteor.

Upvotes: 4

Views: 171

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21384

Does this not work (on the client)?

var loaded = false;
Deps.autorun(function() {
    if (!loaded && isAdmin(Meteor.userId())) {
        $.getScript("/js/moment.min.js");
        loaded = true;
    }
});

Upvotes: 3

Related Questions