如何在我的插件之外使用递归jQuery模板(tmpl)?

| 我有一个转储到jQuery模板(tmpl)中的Json对象。它需要递归,因为Json对象可以是n级深的。此代码有效。但是,如果我将其包装在一个函数中(它属于我正在编写的jQuery插件中),它将停止工作。要了解我的意思,只需在Javascript中取消注释函数包装。 玩它:http://jsfiddle.net/dbme/tkdZg/6/ HTML:
<script id=\"evntTemplate\" type=\"text/x-jquery-tmpl\">

{{if data.identifiers}}
<div id=\"${data.identifiers.id}\" class=\"bsa-event\">
    type: ${data.identifiers.type}<br />
    id: ${data.identifiers.id}<br />
    {{if children}}
        {{each(i, child) children}}
        <blockquote>
            <p>        
            {{if children}}
               {{tmpl(children) \"evntTemplate\"}}
            {{/if}}
            </p>
        </blockquote>
        {{/each}}
    {{/if}}  
</div>
{{/if}}
</script>
<div id=\"eventList\"></div>
Javascript:
//(function ($) {
//    $.fn.SomePlugin = function() {
var movies = {
    \"data\": {
        \"identifiers\":{
            \"type\":0, \"id\":\"makeunique_907827h\"
        }
    },
    \"children\": {
        \"data\": {
            \"identifiers\": {
                \"type\":1, \"id\":\"makeunique_716786g\"
            }
        },
        \"children\": {
            \"data\": {
                \"identifiers\": {
                    \"type\":1, \"id\":\"makeunique_234355g\"
                }
            }
        }        
    }
};

/* Render the template with the data */
var evntTemplate = $( \"#evntTemplate\" ).template( \"evntTemplate\" );
$.tmpl(evntTemplate, movies).appendTo(\"#eventList\");
    //};
//}( jQuery ));
更多信息:我正在使用RequireJS加载文件并启动整个过程。我认为这是杀死范围的原因。这是该代码:
require([\"jquery\", \"jquery.tmpl\", \"jquery.someplugin\"], function($) {
    $(function() {
        $(\'body\').SomePlugin();
    });
});
    
已邀请:
这是因为您没有将jquery对象作为参数传递给函数包装器。 工作示例:http://jsfiddle.net/tkdZg/3 更改为:
(function($) {
var movies = {
    \"data\": {
        \"identifiers\":{
            \"type\":0, \"id\":\"makeunique_907827h\"
        }
    },
    \"children\": {
        \"data\": {
            \"identifiers\": {
                \"type\":1, \"id\":\"makeunique_716786g\"
            }
        },
        \"children\": {
            \"data\": {
                \"identifiers\": {
                    \"type\":1, \"id\":\"makeunique_234355g\"
                }
            }
        }        
    }
};

/* Render the template with the data */
var evntTemplate = $( \"#evntTemplate\" ).template( \"evntTemplate\" );
$.tmpl(evntTemplate, movies).appendTo(\"#eventList\");
})($); //########################
    

要回复问题请先登录注册