5

Checkout the code below. The question is in the comments.

angular.module('MainStreetMower.services', ['ngResource'])
.factory('Videos', function($resource) {
    return $resource('/api/jobs/1/');
});
function VideoListCtrl($scope, Videos) {
    $scope.videos = Videos.query();
    $scope.what = function() {
        // proper way to push to the videos array and $save() the new array.
    }
}
1
  • 2
    I think you need to describe what you want more clearly Commented Jan 4, 2013 at 9:48

1 Answer 1

8

I would say the following:

function VideoListCtrl($scope, Videos) {
    $scope.videos = Videos.query();

    $scope.what = function() {

        var newVideoData = {}; // prepare new video data here from the model
        new Videos(newVideoData).$save(function(video){
          $scope.videos.push(video);
        }); 

    }
}

if you don't want to refresh the whole list. Alternatively you could re-query the collection in the save callback is you expect changes from other sources:

new Videos(newVideoData).$save(function(video){
   $scope.videos = Videos.query();
});

Please note that you could use the save method on the class level. For example, the code above could re-written as:

Videos.save(newVideoData, function(video){
   $scope.videos = Videos.query();
});
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, duh. I need new Videos(). I should have asked how do I access $scope.videos to $save() an updated video. But, that totally changes the question...
Now you got me confused :-) What is the real question then? Do my explanations help you or not?
I'll ask a new related question eventually. Your answer solves the problem stated so it's answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.