jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 在Underscore模板中使用JSON对象 – Backbone.js大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个日历风格的骨干应用程序,但我对它很新.我现在已经对这个问题进行了超过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 @L_944_4@."
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:

[{"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,which was created for the bliss of souls like @L_944_4@.","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","created":"1153074004","ordering":"3","assocs":"NAADAC","abbr":"acn","created_at":"2006-07-16 11:20:04","pivot":{"id":"6","discipline_id":"8"}},{"id":"13","label":"Massage Therapist","created":"0","ordering":"6","assocs":null,"completion_only":"1","abbr":"mass","created_at":"2006-07-18 12:01:31","pivot":{"id":"7","discipline_id":"13"}}],"states":[{"id":"38","code":"OR","name":"Oregon","country_code":"US","state_id":"38"}},{"id":"5","code":"CA","name":"California","state_id":"5"}}],"messages":[],"markeTing_materials":[],"days":[{"id":"1","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 not not defined

我筋疲力尽,教自己骨干比人们想象的更难.任何让这个球再次滚动的帮助都会很棒谢谢你.

解决方法

修改视图

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

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>

大佬总结

以上是大佬教程为你收集整理的jquery – 在Underscore模板中使用JSON对象 – Backbone.js全部内容,希望文章能够帮你解决jquery – 在Underscore模板中使用JSON对象 – Backbone.js所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。