Angular - resource object sometimes undefined?

I have a simple application using Angular that transfers REST resources to Tastypie.

I have a resource defined with a factory:

app.factory("Task", function($resource) {
    return $resource("/api/v1/task/:id/", {scheduling: '@scheduling', meta_only: '@meta_only'});
  });

      

and a simple controller with several functions:

app.controller("TasksController", function($scope, Task){

   Task.get(function(data){ 
       $scope.tasks = data.objects;
   });

   $scope.markAsDone = function(task){
       task.is_done = true;
       task.$save();
   }
}

      

The view has a simple ng task replay and a checkbox that calls markAsDone (task) on ng-change. There are several other similar simple functions.

Now from time to time I get the error "undefined is not a function" when calling a task. $ save (). I can't figure out when and why I do and don't get this error as it sometimes works as expected. I was able to register a "task" in a function in both cases and confidently when an error occurs it is an object, and when not, it has all these properties and functions defined in Angular.

What am I missing?

+3


source to share


1 answer


Add check as



$scope.markAsDone = function(task){
   if(typeof task !== undefined){
      task.is_done = true;
      task.$save();
   }
}

      

0


source







All Articles