1 MyApp = new Backbone.Marionette.Application();
7 AngryCat = Backbone.Model.extend({
13 this.set('votes', this.get('votes') + 1);
17 this.set({rank: this.get('rank') - 1});
20 rankDown: function() {
21 this.set({rank: this.get('rank') + 1});
25 AngryCats = Backbone.Collection.extend({
28 initialize: function(cats){
30 _.each(cats, function(cat) {
31 cat.set('rank', rank);
35 this.on('add', function(cat){
36 if( ! cat.get('rank') ){
37 var error = Error("Cat must have a rank defined before being added to the collection");
38 error.name = "NoRankError";
45 MyApp.on("rank:up", function(cat){
46 if (cat.get('rank') === 1) {
47 // can't increase rank of top-ranked cat
52 self.trigger("reset");
55 MyApp.on("rank:down", function(cat){
56 if (cat.get('rank') === self.size()) {
57 // can't decrease rank of lowest ranked cat
62 self.trigger("reset");
65 MyApp.on("cat:disqualify", function(cat){
66 var disqualifiedRank = cat.get('rank');
67 var catsToUprank = self.filter(
68 function(cat){ return cat.get('rank') > disqualifiedRank; }
70 catsToUprank.forEach(function(cat){
73 self.trigger('reset');
77 comparator: function(cat) {
78 return cat.get('rank');
81 rankUp: function(cat) {
82 // find the cat we're going to swap ranks with
83 var rankToSwap = cat.get('rank') - 1;
84 var otherCat = this.at(rankToSwap - 1);
91 rankDown: function(cat) {
92 // find the cat we're going to swap ranks with
93 var rankToSwap = cat.get('rank') + 1;
94 var otherCat = this.at(rankToSwap - 1);
102 AngryCatView = Backbone.Marionette.ItemView.extend({
103 template: "#angry_cat-template",
105 className: 'angry_cat',
108 'click .rank_up img': 'rankUp',
109 'click .rank_down img': 'rankDown',
110 'click a.disqualify': 'disqualify'
113 initialize: function(){
114 this.listenTo(this.model, "change:votes", this.render);
118 this.model.addVote();
119 MyApp.trigger("rank:up", this.model);
122 rankDown: function(){
123 this.model.addVote();
124 MyApp.trigger("rank:down", this.model);
127 disqualify: function(){
128 MyApp.trigger("cat:disqualify", this.model);
129 this.model.destroy();
133 AngryCatsView = Backbone.Marionette.CompositeView.extend({
136 className: "table-striped table-bordered",
137 template: "#angry_cats-template",
138 itemView: AngryCatView,
140 initialize: function(){
141 this.listenTo(this.collection, "sort", this.renderCollection);
144 appendHtml: function(collectionView, itemView){
145 collectionView.$("tbody").append(itemView.el);
149 MyApp.addInitializer(function(options){
150 var angryCatsView = new AngryCatsView({
151 collection: options.cats
153 MyApp.mainRegion.show(angryCatsView);
156 $(document).ready(function(){
157 var cats = new AngryCats([
158 new AngryCat({ name: 'Wet Cat', image_path: 'assets/images/cat2.jpg' }),
159 new AngryCat({ name: 'Bitey Cat', image_path: 'assets/images/cat1.jpg' }),
160 new AngryCat({ name: 'Surprised Cat', image_path: 'assets/images/cat3.jpg' })
163 MyApp.start({cats: cats});
165 cats.add(new AngryCat({
167 image_path: 'assets/images/cat4.jpg',
168 rank: cats.size() + 1