0

If I call a template from JavaScript with:

namespace.template(record);

where record is an instance of:

/** @typedef {{var: string}} */

and I use the Google Closure Compiler in advanced mode, which rewrites var, then how can I receive var in the template? Is using 'var' instead of var to prevent rewriting the only way to do this?

I'm thinking that, ideally, it would be nice if I could provide types for the template's @params, just like I can for the JavaScript code, which should let the compiler know what rewritten name to use...


It looks like this doesn't happen for any value of var. It does happen though if the key is named default, because SoyToJsSrcCompiler generates code using 'default' (for this special name only, not for any other name I used so far), thus preventing Closure Compiler from renaming it, but the property is renamed in the JavaScript code, because I'm using default without quotes.

rid
  • 61,078
  • 31
  • 152
  • 193

1 Answers1

1

If your template looks something like:

/**
 * Foo
 * @param rec
 */
{template .Foo}
    <div>{$rec.var}</div>
{/template}

Then the SoyToJsSrcCompiler will produce code similar to:

/**
 * @param {Object.<string, *>=} opt_data
 * @param {(null|undefined)=} opt_ignored
 * @return {string}
 * @notypecheck
 */
namespace.Foo = function(opt_data, opt_ignored) {
  return '<div>' + soy.$$escapeHtml(opt_data.rec.var) + '</div>';
};

This generated template function is meant to be included in the compilation with your source file. Since the output property access is of the form rec.var the compiler should rename it correctly.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • Indeed, this seems to work (as I would have expected it to). But now I see what the problem really is. If a property is named `default`, then the code generated by SoyToJsSrcCompiler writes it as `['default']`, but the property gets renamed in the original object by the Closure Compiler... Maybe this happens because `default` is a reserved word, and I'm guessing that the soy compiler is afraid of breaking the syntax if it's not quoting it (even though `rec.default` is perfectly valid syntax)... Can I work around this in a way that doesn't involve renaming my property? – rid Apr 16 '13 at 14:16
  • What you describe should only happen if in the original template you use syntax like `{$rec['var']}`. In that case, you need to call your template like `namespace.foo({rec: {'var': val}})` to avoid the mixed property access issues you are currently seeing. – Chad Killingsworth Apr 16 '13 at 15:11
  • The template contains something similar to `{$rec.foo.default.bar}`, which generates the code `opt_data.rec.foo['default'].bar`. It seems that it only uses quotes for the name `default`, but not for anything else. – rid Apr 16 '13 at 15:52
  • We've gone from in general, to very specific. Can you please post your template? – Chad Killingsworth Apr 17 '13 at 13:30
  • Sure, I'll create a separate question. – rid Apr 17 '13 at 14:43