Skip to content

Instantly share code, notes, and snippets.

@got5
Created December 15, 2014 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save got5/d6967f57d23c5d2a17a2 to your computer and use it in GitHub Desktop.
Save got5/d6967f57d23c5d2a17a2 to your computer and use it in GitHub Desktop.
Slide71 TP3
(function(){
"use strict";
angular.module('app')
.factory('NewsService',['$resource', function ($resource) {
return $resource('/api/news/:op/:id',{id:'@id'},{
like: {method:'GET',params: {op:'like'}},
random: {method:'GET',params:{op:'random'}}
});
}]);
})();
<script src="vendor/angular-resource/angular-resource.js"></script>
var app = angular.module('app', [ 'ngCookies', 'ngRoute','ngResource', 'ui.bootstrap', 'pascalprecht.translate']);
(function () {
"use strict";
/** Home view controller */
angular.module('app')
.controller('HomeController', ['$scope','NewsService', function ($scope,NewsService) {
$scope.message = "Welcome in our shop!!!";
/** Gets all the news. */
$scope.lstNews = NewsService.query();
/** Gets the news with id = 1. */
$scope.currentNews = NewsService.get({ id: 1 });
$scope.newsOfTheDay = NewsService.random();
/** Creates a new news. */
$scope.addedNews = new NewsService();
/** Increments the number of likes of a news, and saves the changes. */
$scope.addLike = function(news) {
news.$like();
};
/** Deletes a news. */
$scope.deleteNews = function(news) {
news.$delete(function(){
$scope.lstNews.splice($scope.lstNews.indexOf(news),1);
});
};
/** Adds a news. */
$scope.addNews = function() {
$scope.addedNews.$save(function(){
$scope.lstNews.push($scope.addedNews);
$scope.addedNews = new NewsService();
});
};
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment