2

I am trying to get songs from soundcloud, I am using some input to set value and send it to my factory to get all the related list of songs and display it. The issue is the the first time all works correctly, but when I am trying to input new values I am getting same results as first time.

My code looks like:

.controller('DashCtrl', function ($scope, SongsService) {
        $scope.formData = {};
        $scope.searchSong = function () {
            SongsService.setData($scope.formData.songName);
        };

UPDATE

the factory :

.factory('SongsService', function ($rootScope) {
        var List = {};
        List.setData = function (tracks) {

            var page_size = 6;
            SC.get('/tracks', {limit: page_size, linked_partitioning: 1}, function (tracks) {
                // page through results, 100 at a time
                List = tracks;
                $rootScope.$broadcast('event:ItemsReceived');
            });
        };
        List.getItems = function () {
            return List;
        };
        return List;
    }).value('version', '0.1');

Thanks for help!

Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89

3 Answers3

1

It's hard to tell without a plunkr reproducing the issue and showing all your relevant code, but I think your problem is that you're overwriting the List variable in the async answer, and this List (I assume) is the object you originally returned from your factory.

I see two noteworthy concepts here:

An angular factory is a singleton, meaning the factory function will only be called once, before the first injection, and every controller it's injected into will work with the same object reference it returned. If you overwrite this object reference, well, the previous value (which the controller has) is still a reference to the original object.

Edit: In fact, by overwriting List you're creating a new object which doesn't even have a setData method anymore!

You probably want to make List private to SongsService, and return a more complex object from the factory that captures List in a closure, and offers some public getter/setter methods for it. (If you insist on replacing the contents of the returned List variable, empty the object and extend it with the new properties - including this method again. But this is much more work, and not a nice solution.)

Community
  • 1
  • 1
doldt
  • 4,466
  • 3
  • 21
  • 36
0

In Angular Service constructors and Factory methods are singleton objects. You need to return a method that you can call. Your code examples are incomplete so it is hard to tell what is going on. What is returned by your factory method, the List object?

If so, when the first call is completed, it overwrites the List object so that the setData method can't be called a second time. What is the SC object, I can not see in your example how you are injecting it. You probably want to fix that too.

Consider this possible solution.

Service

Songs.$inject = ['$http'];
function Songs($http) {
    this.$http = $http;
}

Songs.prototype.getSongs = function(searchTerm) {
    return this.$http.get('http://someendpoint/songs', {searchTerm: searchTerm});
}
service('songs', Songs);

Controller

DashController.$inect = ['songs'];
functionDashController(songs) {
    this.songs = songs;
    this.results = [];
}

DashController.prototype.searchSongs = function(searchTerm) {
    var self = this;
    this.songs.getSongs(searchTerm).then(function(results) {
        this.results = results;
    });
}
controller('DashController', DashController);

This is example uses the best practice controllerAs syntax explained here: http://toddmotto.com/digging-into-angulars-controller-as-syntax/

Martin
  • 15,820
  • 4
  • 47
  • 56
  • You overwrite the List the first time setData() is called. You will not be able to call it again. – Martin Jul 24 '15 at 17:36
0

I found the issue, I got same results all the time because I didnt use cooreclty the api of soundcloud, I didnt send the title on the api... also you are correct, I should not set the list as empty..I should set some value to the list...

Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89