简体   繁体   English

带下划线和对象文字的javascript范围

[英]javascript scope with underscore and object literal

I have the following code and want to set all my underscore templates to use mustache syntax. 我有以下代码,并希望将所有下划线模板设置为使用胡须语法。 If I move the _.templateSetting outside the function, it doesn't work. 如果我将_.templateSetting移到函数外部,则无法使用。 How would I set this globally? 我将如何全局设置?

thx in advance 提前

arc_eh.mb={
  show_tree:function(){
    // all i want to do is move this outside and set globally 
    //  for all my underscore microtemplates
    _.templateSettings = {
      interpolate : /\{\{(.+?)\}\}/g
    };

    var template=_.template("hello {{ some }}");
    var jt = { "some" : "more-something" };
    //var final=template({ some : "say-something" });
    var final=template(jt);
    console.log(final);

update #1 更新#1

So I've updated your fiddle with the problem I am having here: http://jsfiddle.net/vMHeq/1/ 因此,我在这里遇到的问题已经更新了您的小提琴: http : //jsfiddle.net/vMHeq/1/

I AGREE that you fiddle works fine - I know this is a scope issue that I'm not familiar with. 我同意您的拨弄工作正常-我知道这是我不熟悉的范围问题。 Any help appreciated. 任何帮助表示赞赏。

Basically, you're gonna need to set _.templateSettings options outside of your arc_eh.mb object. 基本上,您将需要在arc_eh.mb对象外部设置_.templateSettings选项。 If you want to use all the power of _.template you'll want to include all three settings. 如果要使用_.template的全部功能,则需要包含所有三个设置。 I'm using {{- }}, {{= }}, and {{ }} for the formatting on this. 我正在使用{{-}},{{=}}和{{}}进行格式化。

_.templateSettings = {
    interpolate: /\{\{\=(.+?)\}\}/gim,
    escape: /\{\{\-(.+?)\}\}/gim,
    evaluate: /\{\{([\s\S]+?)\}\}/gim
};

Set up a simple fiddle to show you what's up. 设置一个简单的小提琴,向您展示最新情况。

http://jsfiddle.net/AbLA8/1/ http://jsfiddle.net/AbLA8/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM