Skip to content

Instantly share code, notes, and snippets.

@jergason
Last active December 14, 2015 02:59
Show Gist options
  • Save jergason/5017432 to your computer and use it in GitHub Desktop.
Save jergason/5017432 to your computer and use it in GitHub Desktop.
Ticket = Backbone.Model.extend({
urlRoot: 'api/Ticket/'
});
TicketView = Backbone.View.extend({
initialize: function(model) {
this.model = model;
this.listenTo(this.model, 'change', this.render.bind(this));
this.render()
},
template: _.template($('#ticket-template').html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
//- ticket list -----------------------------------------------------
TicketList = Backbone.Collection.extend({
model: Ticket,
urlRoot: 'api/Ticket/',
comparator: function(ticket) {
return ticket.get('id');
}
});
TicketListView = Backbone.View.extend({
el: $('#app'),
initialize: function() {
var self = this;
// can be useful to keep these around so you can remove event handlers and stuff on
// child views when this view goes away
var this.ticketViews = [];
var ticketView;
this.collection.fetch({
success: function() {
self.collection.models.forEach(function(item) {
// create a ticket view here?
ticketView = new TicketView(item);
self.ticketViews.push(ticketView);
self.$el.append(ticketView.$el);
});
self.render();
}
});
},
render: function() {
// Your parent view doesn't really have any data of its own to render, so you don't
// need anything here really.
}
});
ticket_list = new TicketList;
ticket_list_view = new TicketListView({ collection:ticket_list });
var ticket_list_view_poller = Backbone.Poller.get(ticket_list_view.collection).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment