0

我想知道为什么这不显示我创建的条目。我正在努力寻找使用 Marionette 的简单教程,所以我选择了此处的愤怒猫教程(http://davidsulc.com/blog/2012/04/15/a-simple-backbone-marionette-tutorial/)并尝试了做一些类似但更简单的事情,这样我就可以更好地理解发生了什么。任何帮助,将不胜感激。

这是 Javascript,我正在使用 Marionette.js

    MyApp = new Backbone.Marionette.Application();

    MyApp.addRegions({
    listBox : "#listBox"
    });

    Entry = Backbone.Model.extend({
    defaults: {
        entry : "Blank"
    },
    });

    EntryList = Backbone.Collection.extend({

    model: Entry
    });

    EntryView = Backbone.Marionette.ItemView.extend({
    template: "entry-template",
    tagName: 'tr',
    className: 'entry'
    });

    EntriesView = Backbone.Marionette.CompositeView.extend({
    tagName: "table",
    template: "#entries-template",
    itemView: EntryView,

    appendHtml: function(collectionView, itemView){
        collectionView.$("tbody").append(itemView.el);      
    }
    });

    MyApp.addInitializer(function(options){
        var entriesView = new EntriesView({
            collection: options.ents
        });

        MyApp.listBox.show(entriesView);
    });

    $(document).ready(function(){
        var ents = new EntryList([
            new Entry({ entry: 'abc' }),
            new Entry({ entry: 'def' }),
      new Entry({ entry: 'ghi' })
  ]);
   MyApp.start({entry: ents});
});

这是html:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>Simple Demo</title>
        <link rel="stylesheet" href="assets/screen.css">
    </head>
    <body>
        <div id = "listBox">
        </div>

        <script type="text/template" id="entries-template">
            <thead>
                <tr class='header'>
                    <th>Entry</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </script>
      <script type="text/template" id="entry-template">
            <td><%- entry %></td>
            <td><button class="delete">Delete</button></td>
      </script>
        <script src="js/lib/jquery.js"></script>
        <script src="js/lib/underscore.js"></script>
        <script src="js/lib/backbone.js"></script>
        <script src="js/lib/backbone.marionette.js"></script>
        <script src="js/demo.js"></script>
    </body>
</html>
4

1 回答 1

0

看来您没有在选项中使用正确的键。你应该有:

var entriesView = new EntriesView({
    collection: options.entry
});

作为旁注,我的“愤怒的猫”教程有点过时了。要了解有关使用 Marionette 视图的基础知识,最好阅读此免费 pdf:http ://samples.leanpub.com/marionette-gentle-introduction-sample.pdf(这是我的Marionette 书籍的免费示例)

此外,您的模板选择器无效:您需要在拥有“entry-template”的地方拥有“#entry-template”。

于 2013-07-02T13:52:38.620 回答