Backbone.Marionette - 视图无法正确呈现

时间:2014-04-22 10:18:11

标签: javascript jquery backbone.js marionette

我正在做一个使用数据渲染元素的示例工作。但我没有得到我的结果。这有什么不对?

任何人帮助我?

这是我的代码:请为现场演示小提琴。

HTML:

<div id="content" class="content"></div>

<script type="text/template" id="list">
   <%= name %>
</script>

使用Javascript:

var data = [
    {"name":"name1","city":"city1","age":"age1","subModel":[
        {"name":"name01","city":"city01","age":"age01","subModel":[
            {"name":"name001","city":"city001","age":"age001"}
        ]},
        {"name":"name02","city":"city02","age":"age02","subModel":[
            {"name":"name002","city":"city002","age":"age002"}
        ]},
        {"name":"name03","city":"city03","age":"age03","subModel":[
            {"name":"name003","city":"city003","age":"age003"}
        ]}
    ]}
];

var myApp = new Backbone.Marionette.Application();


myApp.addRegions({
    mainRegion:"#content"
});

var myModel = Backbone.Model.extend({
    defaults:{
        "name":"no name",
        "city":"chennai",
        "age":"10 months"
    }
});

var myCollection = Backbone.Collection.extend({
    model:myModel
});

var oneView = Backbone.Marionette.ItemView.extend({
    tagName:"li",
    template:"#list"
});

var multiView = Backbone.Marionette.CompositeView.extend({
    tagName:'ul',
    itemView:oneView
});


myApp.on('initialize:after', function(){
    var listView = new multiView({collection:data});
    myApp.mainRegion.show(listView);
});

myApp.start();

Live Demo

1 个答案:

答案 0 :(得分:1)

在这里:http://jsfiddle.net/Cardiff/Y5kwM/

这里有几个错误:

 var multiView = Backbone.Marionette.CompositeView.extend({  
     tagName:'ul',  
     itemView:oneView  
 });  

您需要为compositeView提供模板,否则请使用collectionview。

var listView = new multiView({collection:data});

您不能只将数组设置为集合。你需要一个有效的骨干集合(见小提琴)。