1

What's the recommended way to do this?

1.

factory.updater = function(varObjToBeUpdated){
    $http.post('/url', {})
    .success(function(data){
        for (data_field in data)
            varObjToBeUpdated[data_field] = data[data_field];
    });
} 

...

myFactory.updater($scope.varObjToBeUpdated);

Or 2.,

 myFactory.updater().success(function(data, ..){
    $scope.varObjToBeUpdated = data;
});

...

factory.updater = function(){
    return $http.post('/url', {});
}

Is it ok to to pass a reference scope variable to a factory? I always thought factories as delivering data.

And what's wrong with the second method (if it's less acceptable)?

2
  • What's wrong with what you have? Commented Dec 18, 2013 at 1:15
  • Sorry, I think the question is more clear now. Commented Dec 18, 2013 at 1:23

1 Answer 1

3

I prefer the second approach, as this allows you to just inject the service when you need it across multiple controllers. Use .then to continue the promise pattern:

myFactory.updater().then(function(data, ..){
    $scope.varObjToBeUpdated = data;
});

app.factory('myFactor', function($http) {
    return {
        updater: function() {
            return $http({'/url',}).then(function(result) {
                return result.data;
            });
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.