Askdesigners
Askdesigners

Reputation: 419

trying to send parameters to a $resource factory from a template

I might be missing something basic, but I'm stuck on how to do this.

I have an ng-repeat block:

<div    data-class-block    
    ng-repeat="class in classList(1304)" 
    data-object="class">
</div>

Which is calling a function in my controller:

$scope.classList = function(semester){

  var list = myClassesFactory.list(semester)
  .then(function (data){
    return data.classes;
  }, function (error){
    console.log('ruh roh: error in myclasses controller');
  });

  return list;
};

Which is in turn calling the factory:

myclassesNest.factory('myClassesFactory', ['$http', '$resource', '$q', function ($http,     $resource, $q) {
  var myClassesResource = $resource(':sem/myclasses.json', 
  {}, 
  {'query': {method: 'GET', params: {sem: '@sem'}, isArray: false} });

  var factory = {
  list : function(semester) {
      var deferred = $q.defer();
      myClassesResource.query({sem:semester},
      function (resp) {
        deferred.resolve(resp);
      });
      return deferred.promise;
    }
  };
  return factory;
}]); 

This totally blows up the browser throwing error after error until the tab crashes. lol. Probabaly has to do with the digest cycle, which is admittedly an area of Angular I don't really understand fully.

I wrapped the call to myClassesFactory.list in a function as when I had it directly being assigned to the $scope variable I got an error saying it was an object and not a function.

TLDR; I need to be able to pass in a semester from the template to select what group of class resources to return from the endpoint.

Any help is greatly appreciated!

(also before anyone says to read the $resource docs, I'd like to say that I have several times today, and the examples there don't shed any light on this, as they have the parameters hardcoded into the controllers.)

Upvotes: 1

Views: 78

Answers (1)

Erstad.Stephen
Erstad.Stephen

Reputation: 1035

You are stating explicitly that the thing being returned from the query call is NOT going to be an array. It will throw an error if it is and it seems like it might be since you are looking for classes (the plural screams array to me). If you change this to true does it work?

EDITED You are running into a digest cycle loop. The way digest works is depicted here: http://docs.angularjs.org/guide/concepts#runtime

When your call comes back it triggers a update to the page which will call another to the service which will start the loop all over again.

Better to fix it like you apparently did.

Upvotes: 1

Related Questions