无法获取模型来显示属性

时间:2013-07-02 13:26:52

标签: javascript marionette

我想知道为什么这不显示我创建的条目。我正在努力寻找使用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>

1 个答案:

答案 0 :(得分:0)

您似乎没有在选项中使用正确的密钥。你应该:

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

作为旁注,我的“愤怒的猫”教程有点陈旧。要了解使用Marionette视图的基础知识,最好还是阅读这个免费的pdf:http://samples.leanpub.com/marionette-gentle-introduction-sample.pdf(这是我Marionette book的免费样本)

此外,您的模板选择器无效:您需要拥有“#entry-template”,其中包含“entry-template”。