We're building an application with CanJS and RequireJS. I'm currently trying to package the templates into the optimized app. I have written a small RequireJS plugin which uses the official RequireJS text plugin to load the files and registers them as views:
- define(["lib/require-text", "can/view/ejs"], function(text) {
- var buildMap = {},
- escapeName = function(name){
- return name.replace(/\//g, "_")
- }
- return {
- load: function(name, req, onLoad, config) {
- text.load(name+".ejs", req, function(content){
- if (config.isBuild) {
- buildMap[name] = content;
- onLoad(content)
- } else {
- require(["can/view/ejs"], function(can){
- r = new can.view.ejs(escapeName(name), content)
- onLoad(content)
- })
- }
- }, config)
- },
- write: function(pluginName, moduleName, write){
- if(moduleName in buildMap){
- var content = text.jsEscape(buildMap[moduleName]),
- name = escapeName(moduleName)
- write.asModule(pluginName + "!" + moduleName,
- "define(function () { return can.view.ejs('" + name +"', '" + content + "');});\n")
- }
- }
- }
- })
It can be used like this:
- define([
- "ejs!views/login"
- ], function() {
- var Login = can.Control( {
- init: function(el, options){
- el.html(can.view.render("views_login", options))
- }
- })
This works but has the disadvantage that the templates have to be parsed every time in the client. To improve performance I want to write the template function to the optimized script rather than the original template.
I tried a "write" method like this:
- define(["lib/require-text", "can/view/ejs"], function(text, can) {
- ...
- write: function(pluginName, moduleName, write){
- if(moduleName in buildMap){
- var tmpl = new can.EJS({text: buildMap[moduleName], name: moduleName})
- write.asModule(pluginName + "!" + moduleName, "define("+tmpl.fn+");\n");
- }
- }
- }
- })
Is there a way to compile the template without relying on window?ReferenceError: window is not definedIn module tree:insposo/insposoinsposo/login/logininsposo/common/ejscan/view/ejscan/util/librarycan/util/jquery