Angular Factories

GOALS

- primary objective: move all of the view independent logic into the factory so that the controllers have only the functions and data required for managing the $scope that each controller manages
- refactor the player controller so that it uses a player factory
- skip the view stuff as much as possible 
- skip stats factory as much as possible
- set up an album factory that uses $http

MAIN PROBLEMS: 
how is the main ctrl communicating with album ctrl?  via rootScope. that's how 
now we want to make it so that both of these scopes are talking to a factory?

roadmap

1. pause whats playing now

2. switch current song

3. switch song list 

4. load

5. play (resume)

Pause is just a proxy for the pause method on the audio object

 

Load is also a proxy method 

because it wraps the load method on the audio object

 

 

1. resume is also a proxy method because it wraps the play method on the audio object

2. perhaps worth noting that we could set an even on 'play'

3. so resume and pause just toggle isPlaying

 

moveTo needs  a reference to the object we're returning so we need to name that object and return it instead of returning an anonymous object 

take next and previous from controllers 

get Progress just returns progress 

but we still have to modify progress

to do that we need to copy event listeners from controllers

 

TO FIX PROGRESS

1. grab timeupdate and ended from our controllers

2. modify them: remove scope 

remove 100, inject rootScope, fac.next()

 

NOW MODIFY CONTROLLER

 

convert toggle, forward,  back, getCurrentSong, getProgressand isPlaying to use PlayerFactory and everything else can be removed 

 

 

NOW ALBUM CONTROLLER

 

1. inject PlayerFactory into controller

2. rewrite $scope.start ---> we need to bind

 

 

What Album CTRL should Look like

 

CHANGE HTML 

 

 

 

 

 modify html

1. getProgress

2. start(song, album)

 

INDEX HTML

1. isPlaying 

2. getCurrentSong

 

 

 

IT WORKS!!!

ONE LAST THING: lets create an albumFactory so we can get $http out of our albums controller

remember to add this factory to index.html

 

 

 

 

IT WORKS!!!

album controller:

 

 

 

 

deck

By ayana28

deck

  • 1,509