Created
November 19, 2014 22:29
-
-
Save ohall/dafaf5650cb58c674d29 to your computer and use it in GitHub Desktop.
A very simple BackboneJS view with model http://jsfiddle.net/oakley349/ate0Lpos/4/
This file contains hidden or 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
| <style> | |
| #rootEl { | |
| color:#FFFFFF; | |
| text-align:center; | |
| height:50px; | |
| width:150px; | |
| background-color:red; | |
| } | |
| </style> | |
| <div id="rootEl"><h3>Click Me</h3></div> |
This file contains hidden or 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
| var CoolView = Backbone.View.extend({ | |
| el: $("#rootEl"), | |
| initialize: function () { | |
| this.colors = this.model.get('colors'); | |
| console.log('thingy'); | |
| }, | |
| events: { | |
| 'click': 'clickHandler' | |
| }, | |
| clickHandler: function (evt) { | |
| var color = this.getColor(); | |
| $(this.el).css('background-color', color); | |
| this.render(); | |
| console.log('clicked ' + evt.currentTarget.id); | |
| console.log('new color ' + color); | |
| }, | |
| index: 0, | |
| getColor: function () { | |
| var ret = this.colors[this.index]; | |
| if (this.index < this.colors.length - 1) { | |
| this.index++; | |
| } else { | |
| this.index = 0; | |
| } | |
| return ret; | |
| } | |
| }); | |
| var CoolModel = Backbone.Model.extend({ | |
| defaults: { | |
| colors: ['blue', 'green', 'yellow', 'black'] | |
| } | |
| }); | |
| new CoolView({ | |
| model: new CoolModel({}) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment