Javascript 主干视图未正确地将数据传递到下划线模板
为了让我的模板正确显示视图/模型中的数据,我花了很长时间。我可以从整体上获取对象,但无法访问对象中包含的任何数据。我只是得到一个回应,说这个对象是未定义的 App.js Model.js View.js 杰德 我得到了整个对象,但我无法得到任何其他数据。Javascript 主干视图未正确地将数据传递到下划线模板,javascript,node.js,backbone.js,underscore.js,pug,Javascript,Node.js,Backbone.js,Underscore.js,Pug,为了让我的模板正确显示视图/模型中的数据,我花了很长时间。我可以从整体上获取对象,但无法访问对象中包含的任何数据。我只是得到一个回应,说这个对象是未定义的 App.js Model.js View.js 杰德 我得到了整个对象,但我无法得到任何其他数据。 有什么想法吗?我在休息了一会儿,吃了早餐,第二顿早餐就自己想出来了 发生的是我在Doctor.groupProfile.fetch函数上的链接事件出错。在上面的例子中,它在事件完成之前调用并实例化UI,因此虽然我可以看到控制台数据,但模型当时没
有什么想法吗?我在休息了一会儿,吃了早餐,第二顿早餐就自己想出来了 发生的是我在Doctor.groupProfile.fetch函数上的链接事件出错。在上面的例子中,它在事件完成之前调用并实例化UI,因此虽然我可以看到控制台数据,但模型当时没有所有数据,因此未定义 应该是:
Doctor.groupProfile.fetch().done(function() {
$.when().then(initializeUI);
});
这样,视图将在模型具有数据后实例化
谢谢
var Group = Backbone.Model.extend({
url: getMeetupData('groups'),
parse: function(resp) {
this.name = resp.results[0].name;
this.link = resp.results[0].link;
this.organizer = resp.results[0].organizer;
this.description = resp.results[0].description;
this.photo = resp.results[0].group_photo;
this.city = resp.results[0].city;
this.topics = new TopicCollection(resp.results[0].topics);
return resp;
}
});
var GroupsView = Backbone.View.extend({
el: $('template'),
initialize: function(options) {
this.group = options.group;
},
// Use an external template
template: _.template($('#groupsTemplate').html()),
render: function() {
// Fill the html with the template and the collection
//$(this.el).html(this.template({ tweets: this.collection.toJSON() }));
$(this.el).html(this.template({ group: this.group}));
return this;
}
});
extends ./layout/layout
block content
section(class="row"): div(class="column"): div(class="row")
div(class="large-12 columns")
h1 Mile Hi Who
section(class="row space"): div(class="column")
nav(class="backboneNav"): ul
li(class="large-3 columns about"): a(href="#about") About the Group
li(class="large-3 columns events"): a(href='#events') Upcoming Events
li(class="large-3 pull-3 columns"): a(href='#') Members
section(id="template")
script(type="text/template", id="groupsTemplate")
section(id="swiping", class="row"): div(class="column"): div(class="row")
div(class="large-3 large-centered columns")
|<img id="groupPhoto" src='<%= this.group.get('groupPhoto') %>' />
<% console.log(group); %>
.large-12.columns.main-content.group
h3 <%= group.name %>
p.text <%= group.description %>
section(class="row ajaxSpacing"): div(class="column"): div(class="row")
div(class="large-12 columns main-content", id="templateEvent")
script(type="text/template", id="eventsTemplate")
<% _.each(events, function(event){ %>
| <div class="events">
| <h3><%= event.name %></h3><p>
| <%= new Date(event.time).toDateString() %>
| <%= new Date(event.time).toLocaleTimeString() %>
| <%= event.venue.name %></p>
| <button data-event-id='<%= event.id %>'>See More</button>
| <span><p><%= event.description %></p></span>
| </div>
<% }); %>
Doctor.groupProfile.fetch().done(function() {
$.when().then(initializeUI);
});