diff --git a/js/app/models/sound.js b/js/app/models/sound.js index 2c26daf..ec193ab 100644 --- a/js/app/models/sound.js +++ b/js/app/models/sound.js @@ -11,18 +11,24 @@ define( defaults: { title: "", character: "", - file: "" + file: "", + playing: false }, play: function() { - this.audio = new Audio('sounds/'+this.attributes.file); - this.audio.play(); + if( !this.audio ) { + this.audio = new Audio('sounds/'+this.attributes.file); + } - return this; + this.audio.play(); + this.attributes.playing = true; }, stop: function() { if( this.audio && !this.audio.paused ) { this.audio.pause(); + this.audio.load(); } + + this.attributes.playing = false; } }); diff --git a/js/app/templates/sound.hbs b/js/app/templates/sound.hbs new file mode 100644 index 0000000..61ce216 --- /dev/null +++ b/js/app/templates/sound.hbs @@ -0,0 +1 @@ +{{title}} diff --git a/js/app/templates/sounds.hbs b/js/app/templates/sounds.hbs deleted file mode 100644 index a499703..0000000 --- a/js/app/templates/sounds.hbs +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/js/app/views/sound.js b/js/app/views/sound.js new file mode 100644 index 0000000..2ee4803 --- /dev/null +++ b/js/app/views/sound.js @@ -0,0 +1,31 @@ +define( + 'views/sound', + [ + 'marionette', + 'models/sound', + 'hbs!templates/sound' + ], + function (Marionette, SoundModel, SoundBlockTemplate) { + "use strict"; + + var SoundBlockView = Marionette.ItemView.extend({ + template: SoundBlockTemplate, + model: SoundModel, + tagName: 'li', + ui: { + soundItem: 'a' + }, + events: { + 'click @ui.soundItem': 'playSound' + }, + playSound: function(e) { + e.preventDefault(); + + this.trigger('sound:play'); + + this.model.play(); + } + }); + + return SoundBlockView; +}); diff --git a/js/app/views/soundboard.js b/js/app/views/soundboard.js index b11b0ef..9e2e8cd 100644 --- a/js/app/views/soundboard.js +++ b/js/app/views/soundboard.js @@ -6,7 +6,7 @@ define( 'views/sounds', 'hbs!templates/soundboard' ], - function (Marionette, SoundSearchView, SoundListView, SoundboardTemplate) { + function (Marionette, SoundSearchView, SoundsView, SoundboardTemplate) { "use strict"; var SoundboardView = Marionette.LayoutView.extend({ @@ -18,7 +18,7 @@ define( onShow: function() { this.showChildView('search', new SoundSearchView()); - this.showChildView('list', new SoundListView()); + this.showChildView('list', new SoundsView()); } }); diff --git a/js/app/views/sounds.js b/js/app/views/sounds.js index 01f112f..02b1952 100644 --- a/js/app/views/sounds.js +++ b/js/app/views/sounds.js @@ -5,19 +5,17 @@ define( 'backbone.radio', 'underscore', 'collections/sounds', - 'hbs!templates/sounds' + 'views/sound' ], - function (Marionette, Radio, _, SoundsCollection, SoundListTemplate) { + function (Marionette, Radio, _, SoundsCollection, SoundView) { "use strict"; - var SoundListView = Marionette.LayoutView.extend({ + var SoundsCollectionView = Marionette.CollectionView.extend({ + childView: SoundView, collection: new SoundsCollection(), - template: SoundListTemplate, - ui: { - soundItem: 'li a' - }, - events: { - 'click @ui.soundItem': 'playSound' + tagName: 'ul', + childEvents: { + 'sound:play': 'stopPlayingSound' }, initialize: function() { var that = this; @@ -33,29 +31,19 @@ define( that.render(); }); }, - playSound: function(e) { - var index = $(e.currentTarget).attr('data-sound'), - sound = this.collection.at(index); + stopPlayingSound: function() { + var playingSound = this.collection.findWhere({playing: true}); - e.preventDefault(); - - this.stopCurrentSound(); - - if( sound ) { - this.currentSound = sound.play(); - } - }, - stopCurrentSound: function() { - if( this.currentSound ) { - this.currentSound.stop(); + if( playingSound ) { + playingSound.stop(); } }, serializeData: function () { var viewData = {data: this.data}; - return _.extend(viewData, Marionette.LayoutView.prototype.serializeData.apply(this, arguments)); + return _.extend(viewData, Marionette.CollectionView.prototype.serializeData.apply(this, arguments)); } }); - return SoundListView; + return SoundsCollectionView; });