Skip to content

Instantly share code, notes, and snippets.

@Morgul
Created September 26, 2015 16:18
Show Gist options
  • Save Morgul/297dcab9bf486cae4bbc to your computer and use it in GitHub Desktop.
Save Morgul/297dcab9bf486cae4bbc to your computer and use it in GitHub Desktop.
Vuejs Component Template loader
//----------------------------------------------------------------------------------------------------------------------
/// A simple template loader for Vue components (browserify or electron app)
///
/// @module
//----------------------------------------------------------------------------------------------------------------------
var fs = require('fs');
var path = require('path');
var Vue = require('../vendor/vue/dist/vue');
//----------------------------------------------------------------------------------------------------------------------
var rootPath = path.resolve(__dirname, '..', 'dist');
function loadAsync(componentName, vueOpts)
{
return Vue.component(componentName, function(resolve, reject)
{
if(vueOpts.templateUrl)
{
fs.readFile(path.join(rootPath, vueOpts.templateUrl), 'utf8', function(error, html)
{
if(error)
{
reject(error);
}
else
{
delete vueOpts.templateUrl;
vueOpts.template = html;
resolve(vueOpts);
} // end if
});
}
else
{
resolve(vueOpts);
} // end if
});
} // end loadAsync
//----------------------------------------------------------------------------------------------------------------------
module.exports = { loadAsync: loadAsync };
//----------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment