- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个日历风格的主干应用程序,但对它还很陌生。我已经为此工作了 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 的控制台日志的更好 View 。
更新:这是我的字符串化 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"}]}]
这是我的模板 View :
<script id="days" type="text/template">
<a class="btn small-btn marginRight"></a>
</script>
只是想我会在这里添加,如果我在上面使用模板标签,例如 <% title %>
,我收到错误 Uncaught ReferenceError: title is not defined
我很累,自学 Backbone 比人们想象的要难。任何能让这个球再次滚动的帮助都会很棒,谢谢。
最佳答案
修改 View
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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14993552/
如何检查underscore.js中数组中的元素是否存在?例如,我有['aaa', 'bbb', 'cfp', 'ddd'],并想检查'cfp'是否存在。如果可以,我想显示一些文本。我的以下代码无法正
我有一个 array = [1,2,3,4,5]并想使用 underscore.js 获得累积和数组. 我想要的结果是: [1,3,6,10,15] 我想要数组而不是累积总和 15 作为值。任何帮助,
我正在使用 underscore.js的模板库,我不确定如何在模板中使用逻辑。例如,我想在模板中打印一组标签。最好的方法是什么? Javascript: bunny_data = { name:
我正在尝试将我的对象数组格式化为一个数组以供组件使用。我需要将对象的确切值映射到正确的位置。 例如 我的原始数据: var rawData = [ { name: 'j
我有一个带有键的对象 var obj = { a: { fruit: 'Apple' }, b: { fruit: 'Banana' } } 我想快速将键 (a/b) 移动到作为属性 name 的值
我有一个像这样的 json 数组: myArray=[{ a:1, b:[{c:"x",d:"y"}, {c:"r", d:"s"}...] },
可以说我有这个对象数组。 var initialData = [{Title:"Hat",Price:49.95}, {Title:"Pants",Price:7
在使用 _.findWhere 函数时,我无法弄清楚如何在 underscore.js 中动态设置属性。 这是该函数的文档: findWhere_.findWhere(list, properties
我正在将一个相当大的 php 模板(其中包含基本逻辑的页面)转换为一个 underscore.js 模板。 问题是我一直有错误,并且它是缩小的“编译”版本在抛出错误时没有提供有用的信息或行号。 有没有
在 PHP 中,特别是在 Wordpress 中,__('string') 和 _x('string') 有什么区别? 我正在阅读 Wordpress 文档并感到困惑。以下混淆的好例子来自 Wordp
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭3年前。 社区在上个月审核了是
如何在 underscore.js 中查询日期?我知道我不能做“之间”子句,但请参阅下面的代码作为示例。我的目标是根据用户指定的日期范围将 JSON 数据加载到图表中。 var testdata=[{
$.each(asList(1, 2, 3), new Block() { public void apply(Integer item) { System.out.print
我遇到了在线文档与我在程序中看到的在 GO 代码中访问 C 结构的行为之间的脱节。 go version 说我正在使用: go version go1.4.2 linux/amd64 根据GO CGO
我正在浏览Underscore.js api我注意到 _.escape逃脱& , , " , ' , 和 /字符。让我吃惊的是逃跑/ . 有没有逃避的理由/我不知道的字符? 最佳答案 编辑 : 好吧
我想发送带有几个下划线的字符串 "__hello__its_me_"但 Discord 将其解释为 Markdown 并在我的字符串下划线或斜体。 我知道我必须用反斜杠转义下划线,但在 Visual
我有一个连接到 Visual Studio Online 的 Azure 网站。连接两者时,Visual Studio Online 会在托管构建 Controller 上创建一个持续交付构建运行。此
当我尝试呈现以下模板时,在运行时出现编译错误: Title: Author:
我发现自己经常使用以下模式 var line = "12|John Doe" var pieces = line.split("|") var user = { id : pieces[0],
我正在尝试在主干中使用带下划线的 Handlebars 样式模板(与require.js一起使用)。 我有以下几点: _.templateSettings.interpolate = /\{\{(.+
我是一名优秀的程序员,十分优秀!