Skip to content

Instantly share code, notes, and snippets.

@whisher
Created July 30, 2013 10:06
Show Gist options
  • Save whisher/1cb4c9958cbcd532c673 to your computer and use it in GitHub Desktop.
Save whisher/1cb4c9958cbcd532c673 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Scope</title>
</head>
<body>
<div data-ng-app="servicesApp">
<div data-ng-controller="myCtrl">
<ul>
<li data-ng-click="$log.info(recipe)" data-ng-repeat="recipe in recipes">{{recipe.title}}</li>
</ul>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="./scripts/vendor/angular-resource.min.js"></script>
<script>
var services = angular.module('servicesApp', ['ngResource']);
services.factory('Recipe', function($resource) {
return $resource('./recipes.php?id=:id', {id: '@id'});
});
services.run(function($rootScope,$log){
$rootScope.$log = $log;
});
services.factory('MultiRecipeLoader', function(Recipe, $q) {
return function() {
var delay = $q.defer();
Recipe.query(function(recipes) {
delay.resolve(recipes);
}, function() {
delay.reject('Unable to fetch recipes');
});
return delay.promise;
};
});
services.factory('RecipeLoader',function(Recipe, $q) {
return function() {
var delay = $q.defer();
Recipe.get({id: 1}, function() {
delay.resolve();
}, function() {
delay.reject('Unable to fetch recipe ' + 1);
});
return delay.promise;
};
});
services.controller('myCtrl',function($scope,RecipeLoader){
$scope.recipes =RecipeLoader();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment