Created
November 23, 2011 14:05
-
-
Save Stiivi/1388739 to your computer and use it in GitHub Desktop.
Brewery server app w. backbone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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