Skip to content

Instantly share code, notes, and snippets.

@Stiivi
Created November 23, 2011 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stiivi/1388739 to your computer and use it in GitHub Desktop.
Save Stiivi/1388739 to your computer and use it in GitHub Desktop.
Brewery server app w. backbone
$(function(){ /* jQuery.ready */
var Stream = Backbone.Model.extend({
initialize: function() { /* do nothing */ },
});
window.streamsCollection = Backbone.Collection.extend({
model: Stream,
url: "http://localhost:5000/streams",
});
window.StreamItemView = Backbone.View.extend({
tagName: "li",
template: _.template($('#stream-item-template').html()),
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
})
window.AppView = Backbone.View.extend({
el: $("#breweryapp"),
events: {
},
initialize: function() {
this.streams = new window.streamsCollection();
// this.streams.bind('add', this.refreshStreamList, this);
// window.streams.add([
// {name:"example", label: "Example Stream"},
// {name:"another", label: "Another Stream"}
// ]);
this.streams.fetch({
success:function(collection, response){
console.log('success');
console.log(response);
console.log(collection);
},
error:function(collection, response){
console.log('error');
console.log(response);
console.log(collection);
}
});
console.log(this.streams);
console.log("Streams: " + this.streams.length)
console.log("First: " + this.streams.at(0).get("name"))
this.streams.each(function(stream){
console.log("Loaded stream " + stream.get("name"))
});
},
refreshStreamList: function() {
console.log("Stream list refreshed")
list_element = $("#stream-list")
list_element.empty();
this.streams.each(function(stream){
var view = new window.StreamItemView({model: stream});
list_element.append(view.render().el);
});
}
})
window.App= new AppView;
}) /* jQuery.ready */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment