Skip to content

Instantly share code, notes, and snippets.

@ohall
Created November 19, 2014 22:29
Show Gist options
  • Select an option

  • Save ohall/dafaf5650cb58c674d29 to your computer and use it in GitHub Desktop.

Select an option

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/
<style>
#rootEl {
color:#FFFFFF;
text-align:center;
height:50px;
width:150px;
background-color:red;
}
</style>
<div id="rootEl"><h3>Click Me</h3></div>
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