Advertisement
Guest User

Untitled

a guest
May 20th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //View for @girl, EDIT action
  2. GirlEditView = Backbone.View.extend({
  3.     initialize: function(el, attr) {
  4.         this.variables = attr;
  5.         console.log(attr);
  6.         this.render();
  7.     },
  8.     render: function() {
  9.         var template = _.template( $("#girl_edit").html(), this.variables );
  10.         $(this.el).html( template );
  11.         $("#edit_girl").modal('show');
  12.     }
  13. });
  14.  
  15. //View for @girl
  16. GirlView = Backbone.View.extend({
  17.     initialize: function() {
  18.     },
  19.     template: _.template($('#girl_template').html()),
  20.     render: function() {
  21.         this.$el.html(this.template(this.model.toJSON()));
  22.         return this;
  23.     },
  24.     events: {
  25.         "click p.modify": "modify"
  26.     },
  27.     modify: function() {
  28.         //calls to modify view
  29.         new GirlEditView({el : $("#edit_girl")}, this.variables);
  30.     }
  31. });
  32.  
  33. GirlsView = Backbone.View.extend({
  34.     initialize: function() {
  35.         var _this = this;
  36.         this.collection.each(function(girl) {
  37.             var v = new GirlView({model: girl});
  38.             _this.$el.append(v.render().el);
  39.         })
  40.     }
  41. })
  42.  
  43.  
  44. //One girl from the list
  45. Girl = Backbone.Model.extend({
  46.     initialize: function() {
  47.         //this.view = new GirlView({el : $("#content")}, this.attributes );
  48.     }
  49. });
  50.  
  51. //all the girls
  52. Girls = Backbone.Collection.extend({
  53.     model: Girl,
  54. });
  55.  
  56.  
  57. //do magic!
  58. $(document).ready(function() {
  59.  
  60.     //Underscore template modification
  61.     _.templateSettings = {
  62.         escape : /\{\[([\s\S]+?)\]\}/g,
  63.         evaluate : /\{\[([\s\S]+?)\]\}/g,
  64.         interpolate : /\{\{([\s\S]+?)\}\}/g
  65.     }
  66.  
  67.     //get initial data and fill the index
  68.     var list = [];
  69.     $.getJSON('girls.json', function(data) {
  70.         $.each(data, function(key, val) {
  71.             list.push( new Girl(val) );
  72.         });
  73.         var myGirls = new Girls(list);
  74.         var v = new GirlsView({ collection: myGirls });
  75.     });
  76. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement