thortek/dgm4790-spring2014 — pull request #5 by TylersDesk (2014-02-22 07:12:21+00:00)
Title: Tyler-1.0.3-TempData | Modified services to return a deffered object s w...

| Modified the TempData service to return a deferred object so we are not reliant on controllers.

This way, we can call the function from the tempData service on any controller. In our case we are making an ajax call. The way it was wired, it would try to bind the data from our tempData.json file before the file was downloaded. Then it wouldn't show up.

```javascript
GetData: function(){
var tempDataDeferred = $q.defer();

$http.get('data/tempData.json').success(function(data) {
tempDataDeferred.resolve(data);
});

return tempDataDeferred.promise;
}
```javascript
Then when the ajax completes, the .success method is called and tells the promise we passed through earlier to resolve with the data from the ajax call.

Now we can simply include that as a dependency with our controller, any controller, to access the data by calling the GetData() function from our service.

Then we use the .then method of a promise object and bind the data to the scope:

```javascript
.controller('userCtrl', function($scope, tempDataService) {
//Store our promise in a variable so we can do something when it resolves.
var promiseData = tempDataService.GetData();

//When it resolves, take the data it resolves with (tempData) and place it in the scpope.
promiseData.then(function(tempData) {
$scope.tempData = tempData; //Scope variable of temp data.
});
});
```javascript

There is an even better way to do this, especially if we are relying on multiple ajax calls to populate one object. We can do that when we start making multiple ajax calls from our data store.

Anyhow, I commented out the code that was causing the access error, as well as modified dean-home so it properly binds the tempData object.