Skip to content

Instantly share code, notes, and snippets.

@robbiemu
Created August 25, 2016 12:23
Show Gist options
  • Save robbiemu/c9ce03a90d6f1fa53d3a64ba2e8d20c9 to your computer and use it in GitHub Desktop.
Save robbiemu/c9ce03a90d6f1fa53d3a64ba2e8d20c9 to your computer and use it in GitHub Desktop.
angular dynamic resource manager
angular.module('app').service('Res', ['$rootScope', function ($rootScope) {
var scope = $rootScope
scope._scripts = scope._scripts || []
scope._styles = scope._styles || []
scope._rawScripts = scope._rawScripts || []
scope._rawStyles = scope._rawStyles || []
return {
style: function (inp) {
var hrefs = []
if (typeof inp === 'string') {
hrefs.push(inp)
} else {
hrefs = inp
}
scope._styles = []
for (var i in hrefs) {
console.log('styling page with ' + hrefs[i])
var style = document.createElement('link')
if (!/^https?\:\/\//.test(hrefs[i])) {
style.type = 'text/css'
}
style.href = hrefs[i]
style.rel = 'stylesheet'
if (scope._rawStyles === undefined) {
scope._rawStyles = []
}
scope._rawStyles.push([hrefs[i], style])
scope._styles.push(document.head.appendChild(style))
scope.$on('$destroy', this.clean_styles)
}
},
clean_styles: function () {
var removables = []
for (var key in scope._rawStyles) {
console.log('deleting style ' + scope._rawStyles[key][0])
scope._styles[key].parentNode.removeChild(scope._rawStyles[key][1])
removables.push(key)
}
scope._styles = scope._styles.filter(function (v) { return (removables.indexOf(v) !== -1)? true: false })
scope._rawStyles = scope._rawStyles.filter(function (v) { return (removables.indexOf(v) !== -1)? true: false })
},
script: function (inp) {
var hrefs = []
if (typeof inp === 'string') {
hrefs.push(inp)
} else {
hrefs = inp
}
scope._scripts = []
for (var i in hrefs) {
console.log('loading javascript: ' + hrefs[i])
var script = document.createElement('script')
if (!/^https?\:\/\//.test(hrefs[i])) {
script.type = 'text/javascript'
}
script.src = hrefs[i]
if (scope._rawScripts === undefined) {
scope._rawScripts = []
}
scope._rawScripts.push([hrefs[i], script])
scope._scripts.push(document.head.appendChild(script))
scope.$on('$destroy', this.clean_scripts)
}
},
clean_scripts: function () {
var removables = []
for (var key in scope._rawScripts) {
console.log('deleting script ' + scope._rawScripts[key][0])
scope._scripts[key].parentNode.removeChild(scope._rawScripts[key][1])
removables.push(key)
}
scope._scripts = scope._scripts.filter(function (v) { return (removables.indexOf(v) !== -1)? true: false })
scope._rawScripts = scope._rawScripts.filter(function (v) { return (removables.indexOf(v) !== -1)? true: false })
}
}
}])
@robbiemu
Copy link
Author

robbiemu commented Aug 25, 2016

Can be used in routes like this:

(function(){
    angular.module('app')
        .config(['$routeProvider',
            function config($routeProvider) {
                $routeProvider.
                    when('/dashboard', {
                        templateUrl: 'hipermídia/dashboard.template.html',
                        controller: 'dashboard_controller',
                        resolve: {
                            factory: function (Res) {
                                controllersAndServices(Res, {styles: 'estilos/dashboard.css' ,
                                    scripts: 'scripts/load-image.all.min.js'})
                            }
                        }
                    }).

                otherwise('/')
            }
        ])

    const controllersAndServices = function (Res, Routes) {
        Res.clean_scripts()
        Res.clean_styles()

        if ('scripts' in Routes) {
            Res.script(Routes.scripts)
        }
        if ('styles' in Routes) {
            Res.style(Routes.styles)
        }
    }

})()

The values in the conf object you pass can be arrays or strings, and can point to any valid url (CDN, direct URL, or local)

While unloading is mostly cosmetic (aids in visually discerning which libraries are active), this does the job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment