dougmoscrop / angularjs-templates-brunch

Compile templates in to an AngularJS module as a Brunch plugin
MIT License
5 stars 8 forks source link

function for each template ? #6

Open jmls opened 7 years ago

jmls commented 7 years ago

when building the template cache, it seems to me as if each template is wrapped into it's own function:

(function() {
    var module;

    try {
        // Get current templates module
        module = angular.module('myModule');
    } catch (error) {
        // Or create a new one
        module = angular.module('myModule', []);
    }

    module.run(["$templateCache", function($templateCache) {
        $templateCache.put('myApp/components/form1.html', '<html here>');
    }]);
})();

(function() {
    var module;

    try {
        // Get current templates module
        module = angular.module('myModule');
    } catch (error) {
        // Or create a new one
        module = angular.module('myModule', []);
    }

    module.run(["$templateCache", function($templateCache) {
        $templateCache.put('myApp/components/form2.html', '<html here>');
    }]);
})();

when using other tools, I got the following:

(function() {
    var module;

    try {
        // Get current templates module
        module = angular.module('myModule');
    } catch (error) {
        // Or create a new one
        module = angular.module('myModule', []);
    }

    module.run(["$templateCache", function($templateCache) {
        $templateCache.put('myApp/components/form1.html', '<html here>');
        $templateCache.put('myApp/components/form2.html', '<html here>');
    }]);
})();

which is obviously a lot more concise

Is there any reason for writing out the code this way ?

also, is it possible to supress the check for the module, and just use

module = angular.module('myModule');

The third point, and I am struggling to understand it, is that sometimes the cache does not seem to get the template:

(function() {
    var module;

    try {
        // Get current templates module
        module = angular.module('myModule');
console.log("#1")
    } catch (error) {
        // Or create a new one
        module = angular.module('myModule', []);
console.log("#2")
    }
console.log("#3")
    module.run(["$templateCache", function($templateCache) {
console.log("#4")
        $templateCache.put('myApp/components/form1.html', '<html here>');
    }]);
})();

sometimes I see #1, #3 but not #4 - I've tried adding try catch blocks to no avaial - has anyone seen this sort of behaviour ?