在Underscore模板中使用JSON对象 - Backbone.js

4

我正在开发一个日历风格的骨干应用程序,但是对此还很陌生。我已经花费了超过12个小时的时间,但仍然无法让我的模板使用JSON数据进行填充。

这是我今天编写的一些代码:

模型

var CalendarDay = Backbone.Model.extend({
    defaults: function () {
        return {
            title: "No days for this event",
            done: false
        };
    },
    initialize: function () {}
});

var calendarItem = new CalendarDay({
    urlRoot: URL
});

集合

var Calendar = Backbone.Collection.extend({
    model: CalendarDay,
    url: URL
});

视图

var CalendarView = Backbone.View.extend({
    template: _.template($('#days').html()),
    initialize: function () {
        this.collection = new Calendar();
        this.collection.fetch();
        this.collection.bind("reset", this.render, this);
        this.loadTimes();
    },
    render: function () {
        var JSON = this.collection.toJSON();
        this.$el.html(this.template(JSON));
        console.log(JSON);
    },
    listDays: function () {

    }

});

var calendarView = new CalendarView({
    model: calendarItem
});

这是我从服务器获取的JSON数据:

0: Object
activity_logs: Array[0]
attendee_code: "BBNVKBGT"
attendee_fee: "0"
cego_fee: "0"
certificate_fee: "0"
created_at: "2013-02-13 11:29:03"
days: Array[1]
description: "A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine."
disciplines: Array[3]
done: false
fee_transaction_id: "0"
id: "102"
marketing_materials: Array[0]
messages: Array[0]
name: "My very first event"
organization_id: "1"
start_at: "2013-02-28 00:00:00"
state_id: "38"
states: Array[2]
title: "No days for this event"
updated_at: "2013-02-13 11:29:04"
venue_id: "55"

(来自console.log) 以下是我的控制台日志中JSON的更好视图。 JSON的屏幕截图

更新:下面是我字符串化后的JSON:

    [{"title":"No days for this event","done":false,"id":"102","organization_id":"1","state_id":"38","venue_id":"55","name":"My very first event","description":"A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.","start_at":"2013-02-28 00:00:00","attendee_code":"BBNVKBGT","cego_fee":"0","fee_transaction_id":"0","attendee_fee":"0","certificate_fee":"0","created_at":"2013-02-13 11:29:03","updated_at":"2013-02-13 11:29:04","activity_logs":[],"disciplines":[{"id":"1","label":"Psychologist","desc_text":null,"created":"1152725531","valid":"1","ordering":"-1","assocs":"APA","completion_only":"0","abbr":"psy","created_at":"2006-07-12 10:32:11","updated_at":"0000-00-00 00:00:00","pivot":{"id":"5","created_at":"2013-02-13 11:29:16","updated_at":"2013-02-13 11:29:16","conference_id":"102","discipline_id":"1"}},{"id":"8","label":"Alcohol/Drug Counselor","desc_text":null,"created":"1153074004","valid":"1","ordering":"3","assocs":"NAADAC","completion_only":"0","abbr":"acn","created_at":"2006-07-16 11:20:04","updated_at":"0000-00-00 00:00:00","pivot":{"id":"6","created_at":"2013-02-13 11:29:16","updated_at":"2013-02-13 11:29:16","conference_id":"102","discipline_id":"8"}},{"id":"13","label":"Massage Therapist","desc_text":null,"created":"0","valid":"1","ordering":"6","assocs":null,"completion_only":"1","abbr":"mass","created_at":"2006-07-18 12:01:31","updated_at":"0000-00-00 00:00:00","pivot":{"id":"7","created_at":"2013-02-13 11:29:16","updated_at":"2013-02-13 11:29:16","conference_id":"102","discipline_id":"13"}}],"states":[{"id":"38","code":"OR","name":"Oregon","country_code":"US","pivot":{"id":"6","created_at":"2013-02-13 11:29:16","updated_at":"2013-02-13 11:29:16","conference_id":"102","state_id":"38"}},{"id":"5","code":"CA","name":"California","country_code":"US","pivot":{"id":"5","created_at":"2013-02-13 11:29:16","updated_at":"2013-02-13 11:29:16","conference_id":"102","state_id":"5"}}],"messages":[],"marketing_materials":[],"days":[{"id":"1","conference_id":"102","happens_at":"2013-02-28 00:00:00","start_at":"0000-00-00 00:00:00","end_at":"0000-00-00 00:00:00","created_at":"2013-02-20 12:37:23","updated_at":"2013-02-20 12:37:23"}]}] 

这是我的模板视图:
    <script id="days" type="text/template">
            <a class="btn small-btn marginRight"></a>
    </script>

我想在这里补充一下,如果我在上面使用像 <% title %> 这样的模板标签,会出现错误 Uncaught ReferenceError: title is not defined

我很累,自学backbone比人们想象中的更难。任何帮助让这个项目再次开始将是非常棒的。谢谢。


如果你还没有看过的话,这是一个很好的指南,如果你已经非常了解 JQuery: https://github.com/kjbekkelund/writings/blob/master/published/understanding-backbone.md - Plynx
在控制台中,执行console.log(JSON.stringify(the_json_response))并更新你的问题,把该字符串化(stringified)的JSON贴到jsfiddle或类似的工具中以便进行处理。 - Cymen
你的模板只有一个标签?它不会发出除 <a class="btn small-btn marginRight"></a> 之外的任何内容。你需要附加一些数据元素(并处理它是集合的事实)... http://underscorejs.org/#template - WiredPrairie
1个回答

5

修改视图

this.$el.html(this.template(JSON));

to

this.$el.html(this.template({days: JSON}));

修改模板
<script id="days" type="text/template">
    <% _.each(days, function(day) { %> <a class="btn small-btn marginRight"><%= day.title %></a> <% }); %>
</script>

看起来这里走在正确的轨道上,非常感谢。不过,它只返回我在“CalendarDay”模型中设置的默认值“No days for this event”。我该如何让它显示在“happens_at”JSON字段中? - Mike Lucid
@MikeLucid 试试这个模板:<% _.each(days, function(day) { %> <a class="btn small-btn marginRight"><%= day.title %> 在 <%= day.days[0].happens_at %></a> <% }); %> - anhulife

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接