Created
November 14, 2012 09:30
-
-
Save truncs/4071195 to your computer and use it in GitHub Desktop.
backup 5
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>I have a back bone</title> | |
</head> | |
<body> | |
Name: <input type="text" name="name" id="name"/> | |
Age: <input type="text" name="address" id="address"/> | |
<button class="post">Create</button> | |
<ul id="location-list"> | |
</ul> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> | |
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script> | |
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script> | |
<script src="/static/js/template.js"></script> | |
<script> | |
(function ($) { | |
var TEMPLATE_URL = '/static'; | |
Location = Backbone.Model.extend({ | |
urlRoot: "/locations/", | |
}); | |
Locations = Backbone.Collection.extend({ | |
model: Location, | |
url: '/locations/', | |
parse: function(resp) { | |
return resp; | |
} | |
}); | |
LocationView = Backbone.View.extend({ | |
tagName: "li", | |
render: function() { | |
var self = this; | |
$(self.el).template(TEMPLATE_URL + '/templates/item.html', self.model.toJSON(), function() { | |
self.setText(); | |
}); | |
return this; | |
}, | |
setText: function() { | |
var text = this.model.get('name'); | |
this.$('.location-text').text(text); | |
}, | |
}); | |
AppView = Backbone.View.extend({ | |
el: $("body"), | |
locations: new Locations(), | |
initialize: function (options) { | |
var self = this; | |
self.locations.bind('add', this.addOne, this); | |
self.locations.bind('reset', this.addAll, this); | |
self.locations.add($.parseJSON({{locations|tojson|safe}})); | |
alert(JSON.stringify(self.locations)); | |
}, | |
events: {'click .post': 'createOnSubmit', | |
'reset': 'addAll'}, | |
addOne: function(location) { | |
var view = new LocationView({model: location}); | |
$("#location-list").append(view.render().el); | |
$('#name').val(''); | |
$('#address').val(''); | |
}, | |
addAll: function() { | |
this.locations.each(this.addOne); | |
}, | |
createOnSubmit: function(){ | |
alert(JSON.stringify(this.locations)); | |
var location_name = $('#name').val(); | |
var location_address = $('#address').val(); | |
// alert(location_address); | |
this.locations.create({name: location_name, address: location_address}); | |
} | |
}); | |
var appview = new AppView; | |
})(jQuery); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment