1
0
mirror of https://github.com/2ec0b4/kaamelott-soundboard.git synced 2025-12-08 15:43:24 +00:00

Révise la gestion des sons via une CollectionView

This commit is contained in:
Antoine
2016-04-10 23:43:09 +02:00
parent a25e921055
commit 36935c6c80
6 changed files with 57 additions and 38 deletions

View File

@@ -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;
}
});

View File

@@ -0,0 +1 @@
<a href="#">{{title}}</a>

View File

@@ -1,7 +0,0 @@
<ul>
{{#each data.sounds}}
<li>
<a href="#" data-sound="{{@key}}">{{title}}</a>
</li>
{{/each}}
</ul>

31
js/app/views/sound.js Normal file
View File

@@ -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;
});

View File

@@ -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());
}
});

View File

@@ -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;
});