Skip to content

Instantly share code, notes, and snippets.

@DavidSouther
Created January 13, 2015 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidSouther/caeb675c5e9efcaf94ae to your computer and use it in GitHub Desktop.
Save DavidSouther/caeb675c5e9efcaf94ae to your computer and use it in GitHub Desktop.
Song flux theory post
angular.module('trkstr.player.actions', [
'songFlux'
]).factory('PlayerActions', function(songFactory){
function PlayAction(track){
this.track = track;
this.purpose = 'Request a track be played.';
this.dispatcher = songFactory.getDispatcher('trkstr');
}
PlayAction.prototype.dispatch = function(){
this.dispatcher.dispatch(this);
};
return {
Play: PlayAction
};
});
function LibraryController(Library, Actions) {
this.Library = Library; // A library has albums, which have tracks
this.albums = Library.albums; // Updates on Library mutation events
this.Actions = Actions;
}
LibraryController.prototype.play = function(track) {
return this.Actions.Play(track).dispatch();
};
LibraryController.$inject = ['TrkstrLibrary', 'TrkstrActions'];
<ol ng-repeat="album in state.albums">
<li>{{ album.name }}</li>
<li
ng-repeat="track in album.tracks"
ng-click="state.play(track)"
>{{ track.title }}</li>
</ol>
function PlayerController(store) {
this.store = store;
this.store.on(this.store.Events.TrackChanged, this.play.bind(this));
this.play();
}
PlayerController.prototype.play = function() {
return this.track = this.store.currentTrack;
};
PlayerController.$inject = ['PlayerStore'];
function PlayerDirective() {
this.controller = PlayerController;
this.templateUrl = 'player';
this.replace = false;
this.restrict = 'E';
this.controllerAs = 'state';
this.bindToController = true;
this.scope = {};
}
PlayerDirective.factory = function() {
return new PlayerDirective();
};
PlayerDirective.factory.$inject = [];
angular.module('trkstr.player.component', [
'trkstr.stores.player', 'player.template'
]).directive('player', PlayerDirective);
PlayerFactory = function(Actions, song) {
function PlayerStore() {
global.EventEmitter.call(this);
this.dispatcher = song.getDispatcher('trkstr');
this.Events = PlayerStore.Events;
this.currentTrack = { title: "Nothing Playing..." };
this.doPlay = this.dispatcher.register(Actions.Play, this.play.bind(this));
}
PlayerStore.prototype = Object.create(EventEmitter.prototype);
PlayerStore.prototype.play = function(playAction) {
this.currentTrack = playAction.track;
this.emit(PlayerStore.Events.TrackChanged);
};
PlayerStore.Events = {
TrackChanged: 'TrackChanged'
};
return new PlayerStore();
};
PlayerFactory.$inject = [ 'TrkstrActions', 'songFactory' ];
angular.module('trkstr.stores.player', [
'trkstr.actions', 'songFlux'
]).factory('PlayerStore', PlayerFactory);
<!-- PlayerController Template -->
<div class="Player">
<pre class="title">playing {{ state.track.title }}</pre>
<audio
ng-if="state.track.path"
autoplay controls
ng-src="{{ state.track.path }}"
/>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment