Last active
April 13, 2016 08:57
-
-
Save deyceg/c509c1d67bc779f28ca59ad917b807b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LibraryCtrl($scope, LibraryService, artists) { //assumming you're favoring $scope over this | |
$scope.artists = artists || []; //defensive | |
$scope.albums = []; | |
$scope.selectedArtist = null; | |
activate(); //I like to put my activation logic in a function, and call it | |
function activate() { | |
$scope.selectedArtist = LibraryService.getSelectedArtist(); | |
if ($scope.selectedArtist) { | |
//fetch albums | |
LibraryService | |
.loadAlbums($scope.selectedArtist) //this will persist the results (no point fetching again if the selected artist doesnt change) | |
.then(updateAlbums) //resolve value from loadAlbums is probably going to be an array of albums :) | |
.catch(handleErrorInView) | |
} | |
} | |
function updateAlbums(albums) { | |
//update ViewModel | |
$scope.albums = albums; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function libraryService($http) { | |
var selectedArtist = null; | |
var selectedAlbum = null; | |
var allArtists = []; | |
var albumsForArtist = []; | |
var svc = { | |
getSelectedArtist: getSelectedArtist, | |
getSelectedAlbum: getSelectedAlbum, | |
loadArtists: loadArtists, | |
loadAlbums: loadAlbums, | |
} | |
return svc; | |
function getSelectedArtist() { | |
return selectedArtist | |
} | |
function getSelectedAlbum() { | |
return selectedAlbum | |
} | |
function loadArtists() { | |
return $http //return the $promise so we can chain/intercept | |
.get('/path/to/resource') | |
.then(function(response) { | |
allArtists = response.data; | |
}) | |
.catch(maybeRetry); | |
} | |
function loadAlbums(artists) { | |
return $http //return the $promise so we can chain/intercept | |
.get('/path/to/resource') | |
.then(function(response) { | |
// *** the variable ref albumsForArtist is now pointing to response.data *** | |
albumsForArtist = response.data; | |
return albumsForArtists; // Passing the variable ref back to the controller *** if you change modify it to point to a new object you WILL lose data binding *** | |
}) | |
.catch(maybeRetry); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$stateProvider.state({ | |
name: 'app.lib', | |
url: 'lib', | |
controller: 'LibraryController', | |
controllasAs: 'vm', | |
resolve: { | |
artists: function(LibraryService) { | |
return LibraryService.loadArtists(); //returns $promise, resolve value injected into $controller | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment