JSFiddle - React, Tailwind, and code Playground
by chrisfrisina
JavaScript
//filename: views/beats/beatView.js
/*
This is the view for a single beat, which
is contained in a measure view.
*/
define([
'jquery',
'underscore',
'backbone',
'backbone/models/beat',
'text!backbone/templates/measures/audioMeasures.html',
'text!backbone/templates/beats/linearBarBeats.html',
'text!backbone/templates/beats/linearBarSVGBeats.html',
'text!backbone/templates/beats/circularPieBeats.html',
'app/dispatch',
'app/log'
], function($, _, Backbone, BeatModel, audioMeasuresTemplate, linearBarBeatsTemplate, linearBarSVGBeatsTemplate, circularPieBeatsTemplate, dispatch, log){
return Backbone.View.extend({
el: $('.beat'),
//registering backbone's click event to our toggle() function.
events : {
'click' : 'toggle'
},
that: this,
// The different representations
representations: {
"audio": audioMeasuresTemplate,
"linear-bar": linearBarBeatsTemplate,
"linear-bar-svg": linearBarSVGBeatsTemplate,
"circular-pie": circularPieBeatsTemplate
},
currentBeatRepresentation: 'linear-bar',
beatAngle: 90,
//The constructor takes options because these views are created
//by measuresView objects.
initialize: function(options){
console.log('options :');
console.log(options);
if (options) {
this.model = options.model;
this.el = options.el;
} else {
this.model = new BeatModel;
}
this.render();
},
//We use css classes to control the color of the beat.
//A beat is essentially an empty div.
render: function(){
var state = this.bool();
var compiledTemplate = _.template(this.representations[this.currentBeatRepresentation], {beat: this.model, beatAngle: this.beatAngle, state: state});
console.log(compiledTemplate)
$(this.el).append(compiledTemplate);
return this;
},
bool: function(){
if (this.model.get("selected")) {
return "ON";
} else {
...