无法获取要显示属性的模型

Cannot get models to display attributes

本文关键字:属性 模型 显示 获取      更新时间:2023-09-26

我想知道为什么没有显示我创建的条目。我很难找到使用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>

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

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

顺便说一句,我的"愤怒的猫"教程有点过时了。要了解有关使用Marionette视图的基本知识,您最好阅读此免费pdf:http://samples.leanpub.com/marionette-gentle-introduction-sample.pdf(这是我的木偶书的免费样本)

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