Browse Source

Solve the intermittent issue of _ not being defined in templates

JC Brand 8 năm trước cách đây
mục cha
commit
738d227297
2 tập tin đã thay đổi với 21 bổ sung5 xóa
  1. 8 5
      src/config.js
  2. 13 0
      src/lodash.noconflict.js

+ 8 - 5
src/config.js

@@ -36,7 +36,6 @@ require.config({
         "strophe.rsm":              "node_modules/strophejs-plugin-rsm/strophe.rsm",
         "strophe.vcard":            "node_modules/strophejs-plugin-vcard/strophe.vcard",
         "text":                     "node_modules/text/text",
-        "tpl":                      "node_modules/lodash-template-loader/loader",
         "typeahead":                "components/typeahead.js/index",
         "lodash":                   "node_modules/lodash/lodash",
         "lodash.noconflict":        "src/lodash.noconflict",
@@ -163,11 +162,15 @@ require.config({
     },
 
     packages: [{
-        name: 'moment',
+        'name': 'moment',
         // This location is relative to baseUrl. Choose bower_components
         // or node_modules, depending on how moment was installed.
-        location: 'node_modules/moment',
-        main: 'moment'
+        'location': 'node_modules/moment',
+        'main': 'moment'
+    }, {
+        'name': 'tpl',
+        'location': 'node_modules/lodash-template-loader',
+        'main': 'loader'
     }],
 
     map: {
@@ -198,6 +201,6 @@ require.config({
 
     // define module dependencies for modules not using define
     shim: {
-        'awesomplete':          { exports: 'Awesomplete' },
+        'awesomplete':          { exports: 'Awesomplete' }
     }
 });

+ 13 - 0
src/lodash.noconflict.js

@@ -1,4 +1,17 @@
 /*global define */
 define(['lodash'], function (_) {
+    if (!_.isUndefined(require)) {
+        /* XXX: This is a hack to make sure that the compiled templates have
+         * access to the _ object.
+         *
+         * Otherwise we sometimes get errors like this:
+         *
+         *    TypeError: Cannot read property 'escape' of undefined
+         *     at eval (./src/templates/chatroom_sidebar.html:6)
+         */
+        var lodashLoader = require.s.contexts._.config.lodashLoader;
+        lodashLoader.templateSettings.imports = { '_': _ };
+        require.config({'lodashLoader': lodashLoader});
+    }
     return _.noConflict();
 });