MiroHibler / meteor-preloader

A Meteor "lazy-loader" for external .js and .css libraries
75 stars 3 forks source link

Implementation, external.js not firing #6

Closed D1no closed 9 years ago

D1no commented 9 years ago

I very much like the concept, but I can not get it to work. (Compatibility?)

Since you don't have any tests, I simply added it to a baseline set-up https://github.com/D1no/preloadertest to check out for recreation. Preloader was added to the global configuration object Router.configure; router.js

A blind copy of the examples including a variation in objects async, sync, common, default and key expressions ' ', or file root / seems to have no effect. Why does external.js not fire?

// Global Router Set-Up
Router.configure({
    layoutTemplate: 'Layout', 
    loadingTemplate: "Loading",
/*
    miro:preloader preload router extension
*/
    preload: {
        sync: {
            'default': {
                'js'     : '/external.js'
            }
        }
    }
});
MiroHibler commented 9 years ago

Hey Dino, thank you for checking it out. Tests are coming as soon as I find some time to write (and test ;) them, but you're welcome to do it as well :)

As for the problem - it seems that in the latest version(s) Iron.Router doesn't call route event handlers (onRun, waitOn,...) within extended (custom) route controllers unless they are explicitly assigned to particular route.

For now, I'd suggest two workarounds: either...

Router.route( '/', {
    name: 'home',
    // Assign route controller to each route
    // 'PreloadController' is provided by the
    // miro:preloader package
    controller: 'PreloadController'
});

...or...

// NOTE: PreloadController is client-only!
if ( Meteor.isClient ) {
//  HomeController = RouteController.extend({
    HomeController = PreloadController.extend({
        waitOn: function () {
            return [Meteor.subscribe("articles")];
        },
        data: function () {
            return  {
                articles: Articles.find()
            };
        },
        preload: {
            'sync': {
                'default': {
                    'js'     : '/externaljs.js'
                }
            }
        }
    });
}
jadsonlourenco commented 9 years ago

Same here, not working on any place, on controller or out... ;)

MiroHibler commented 9 years ago

Hey Jadson, have you tried the workarounds I've described?

jadsonlourenco commented 9 years ago

yes man, before comment here. :D

MiroHibler commented 9 years ago

Well, that's odd man. I'm using it on one of my projects and I tested it with Dino's preloadertest repository where my workaround code came from.

Ya better check your code again, man ;)

D1no commented 9 years ago

Mh, can you give me a pull-request - it doesn't work for me either

MiroHibler commented 9 years ago

There you go! :)

D1no commented 9 years ago

Hey, thanks for the quick response. Well, i think I found the error. The reason why it works in my repo is due to the double declaration of preloader in both the route and the controller. And you had a typo in the controller, loading externaljs.js.

But I believe there is also an odd behavior that the controller has no effect.

StatusQuo

router.js

// Global Router Set-Up
Router.configure({
    layoutTemplate: 'Layout', 
    loadingTemplate: "Loading",
/*
    miro:preloader preload router extension
*/
    preload: {
        sync: {
            'default': {
                'js'     : '/external.js'
            }
        }
    }
});

controller.js

if ( Meteor.isClient ) {
    // HomeController = RouteController.extend({
    HomeController = PreloadController.extend({
        waitOn: function () {
            return [Meteor.subscribe("articles")];
        },
        data: function () {
            return  {
                articles: Articles.find()
            };
        },
        preload: {
            'sync': {
                'default': {
                    'js'     : '/external.js' //correct file
                }
            }
        }
    });
}

(1/3) Loads pre-loader but not the external.js

deleted preloader settings from router.js router.js

// Global Router Set-Up
Router.configure({
    layoutTemplate: 'Layout', 
    loadingTemplate: "Loading",
/*
    miro:preloader preload router extension
*/
});

controller.js

if ( Meteor.isClient ) {
    // HomeController = RouteController.extend({
    HomeController = PreloadController.extend({
        waitOn: function () {
            return [Meteor.subscribe("articles")];
        },
        data: function () {
            return  {
                articles: Articles.find()
            };
        },
        preload: {
            'sync': {
                'default': {
                    'js'     : '/externaljs.js'
                }
            }
        }
    });
}

(2/3) Loads pre-loader but not the external.js

router.js

// Global Router Set-Up
Router.configure({
    layoutTemplate: 'Layout', 
    loadingTemplate: "Loading",
/*
    miro:preloader preload router extension
*/
    preload: {
        sync: {
            'default': {
                'js'     : '/external.js'
            }
        }
    }
});

deleted preloader settings in controller.js from HomeController. Also changes there have no effect

if ( Meteor.isClient ) {
    // HomeController = RouteController.extend({
    HomeController = PreloadController.extend({
        waitOn: function () {
            return [Meteor.subscribe("articles")];
        },
        data: function () {
            return  {
                articles: Articles.find()
            };
        }
    });
}

(3/3) Loads pre-loader and external.js, but definitions in controller are irrelevant

router.js

// Global Router Set-Up
Router.configure({
    layoutTemplate: 'Layout', 
    loadingTemplate: "Loading",
/*
    miro:preloader preload router extension
*/
    preload: {
        sync: {
            'default': {
                'js'     : '/external.js'
            }
        }
    }
});

As long preload is included, content doesn't matter

if ( Meteor.isClient ) {
    // HomeController = RouteController.extend({
    HomeController = PreloadController.extend({
        waitOn: function () {
            return [Meteor.subscribe("articles")];
        },
        data: function () {
            return  {
                articles: Articles.find()
            };
        },
        preload: {
            'pizza': {
                'american-style': {
                    'dinner'     : 'salami'
                }
            }
        }
    });
}
MiroHibler commented 9 years ago

Hey guys, I've just released a new version of Preloader as well as updated testing app.

API (and internals) is simplified and should be easier to use.

Let me know how it goes.

taromero commented 9 years ago

I worked for me with the following syntax:

Router.route('/', {
  template: 'custom_view',
  controller: PreloadController,
  preload: {
    styles: '/materialize/css/materialize.min.css'
  }
})