下のコードはAngular.jsで通信してデータを取得している処理でしっかり動いているのですが、非常にファットコントローラーになってしまっています。
`
otorecoApp.controller('NewSongListCtrl', ['$scope', '$http', '$sce', function($scope, $http, $sce) {
$scope.newSongs = [];
$http({method: 'GET', url: 'top/getNewSongs'}).
success(function(data, status, headers, config) {
for(var i = 0; i < data.length; i++) {
$scope.newSongs.push(
{
songName: data[i].Song.name,
artistName: data[i].Artist.name,
songNameForSearch: optimize(data[i].Song.name),
artistNameForSearch: optimize(data[i].Artist.name),
artistId: data[i].Artist.id,
naturalLow: returnPitchName(data[i].Song.natural_low),
naturalHigh: returnPitchName(data[i].Song.natural_high),
falsettoLow: returnPitchName(data[i].Song.falsetto_low),
falsettoHigh: returnPitchName(data[i].Song.falsetto_high)
}
);
}
}).
error(function(data, status, headers, config) {
console.log(status);
});
$scope.search = function(index) { $scope.searchResults = []; $scope.googleUrl = []; artistName = $scope.newSongs[index].artistNameForSearch; songName = $scope.newSongs[index].songNameForSearch; $http({method: 'GET', url: 'http://gdata.youtube.com/feeds/api/videos?vq=' + artistName + ' ' + songName + '&max-results=10&alt=json'}). success(function(data, status, headers, config) { $scope.googleUrl.push({url: 'www.google.co.jp/#q=' + artistName + ' ' + songName + '歌詞&qscrl=1'}); for(var i = 0; i < 4; i++) { var rs = data.feed.entry[i].link[0].href.match(/http://www.youtube.com/watch?v=(.*?)&feature=youtube_gdata/); $scope.searchResults.push( { artistName: artistName, songName: songName, movieTitle: data.feed.entry[i].title.$t, movieUrl: $sce.trustAsResourceUrl('http://www.youtube.com/embed/' + rs[1] + '?rel=1&fs=1') } ); } }).error(function(data, status, headers, config) { console.log(status); }); }
}]);
`
そこで通信してる処理だけでも別の関数に切り分けるなりしたいのですが、どうやってやればいいのかわかりません。
良い分け方を知っていましたらご教授お願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。