I am having a horrible time trying to get my template to properly display data from my View/Model. I can get the object overall but I cannot access any data contained in the object. I just get a response saying the object is undefined.
$(document).ready(function() {
var Doctor = Doctor || {};
var startup = function() {
Doctor.groupProfile = new Group();
Doctor.groupProfile.fetch().done(
$.when().then(initializeUI)
);
};
var initializeUI = function() {
Doctor.groupView = new GroupsView({
group: Doctor.groupProfile,
el: '#template'
});
Doctor.groupView.render();
};
startup();
});
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>
<% }); %>
I get the overall object but I cannot ever get any other data. Any ideas?
I figured it out on my own after taking a small break and having breakfast (well second breakfast).
What was happening is I had my chaining event on the Doctor.groupProfile.fetch function wrong. In the case above it was calling and instantiating the UI before the event was finished so while I could see console data the model did not have all the data at the time thus being undefined.
it should be:
Doctor.groupProfile.fetch().done(function() {
$.when().then(initializeUI);
});
This way the view will be instantiated after the Model has data.
Thanks!