如何在骨干中显示HTML竞争.js

how to show html contend in backbone.js

本文关键字:显示 HTML 竞争 js      更新时间:2023-09-26

我正在尝试在主干中加载html文件.js但我无法显示视图.请告诉我做错了哪里..我将与您分享我的代码。 **code** : http://goo.gl/CcqYwX

 $(document).ready(function(){
    var ContactManager = new Marionette.Application();
    ContactManager.addRegions({
        mainRegion:"#contend"
    })
    ContactManager.on("start", function(){
        console.log("ContactManager has started!");

    });
    ContactManager.start();
 // router 
     var routers =  Backbone.Router.extend({
    routes: {
        "": "showFirstPage"
    },
    showFirstPage:function(){
    }
    })
     var ToolItemView = Backbone.Marionette.ItemView.extend({
        template: 'template/test.html',

    });
 var toolItemview = new ToolItemView(); 
 ContactManager.mainRegion.show(toolItemview); 
})

我正在尝试加载测试.html文件,但我无法做到这一点..?

偶默认使用下划线模板。 您需要使用某种外部加载器将它们作为变量加载,或者可以将它们作为脚本元素放置在 DOM 中,然后可以使用模板属性引用这些元素。 看这里:

因此,例如,如果您将其放入html中,则代码将如下所示

<html>
<body>
<script type="text/template" id="example">
<div class="template-content-here">
<%=variable_here %>
<!-- probably more stuff here -->
</div>
</script>
<script src="myApp.js"></script>
</body>
</html>

然后你可以在 JavaScript 中引用它作为

var ToolItemView = Backbone.Marionette.ItemView.extend({
    template: '#example',
 });

这适用于小型项目,对于大型项目,您需要某种构建/模块系统来拉入预编译的模板并直接引用这些模板。

更多信息在这里: http://marionettejs.com/docs/v2.3.1/marionette.renderer.html