Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. // dynamic pages for each ITEM, on ng-click
  2. // from $routeParams.itemID in Ctrl
  3. .when('/:itemID', {
  4. templateUrl: 'views/item.html',
  5. controller: 'ItemElementsController'
  6. })
  7.  
  8. app.get('/api/items/:item_id', function(req, res) {
  9.  
  10. // use mongoose to get the one emotion from the database
  11. Item.findById({
  12. _id : req.params.item_id
  13. },
  14.  
  15. function(err, item) {
  16.  
  17. // if there is an error retrieving, send the error. nothing after res.send(err) will execute
  18. if (err) {
  19. res.json({ error: err });
  20. } else {
  21. res.json(item); // return the item in JSON format
  22. }
  23. });
  24. });
  25.  
  26. angular.module('ItemElementsCtrl', [])
  27.  
  28. // inject the Item service.factory into our controller
  29. .controller('ItemElementsController', function($scope, $routeParams, $location, $http, ItemElements, isEmptyObjectFilter) {
  30.  
  31. // GET by ID ==================================================================
  32. // get an Item after clicking it
  33. $scope.getItem = function(id) {
  34. ItemElements.getOne(id)
  35. // if successful getByID, call our function to get the Item data
  36. .success(function(data) {
  37. // assign our Item
  38. $scope.item = data;
  39. // for use with a parameter in appRoutes.js using itemID as the variable
  40. $scope.itemID = $routeParams.itemID;
  41. // redirect
  42. $location.path('/' + $routeParams.itemID);
  43. })
  44. .error(function(data) {
  45. console.log('Error: ' + data);
  46. });
  47. };
  48. });
  49.  
  50. angular.module('ItemElementsService', [])
  51.  
  52. // super simple service
  53. // each function returns a promise object
  54. .factory('ItemElements', function($http) {
  55. return {
  56. getOne : function(id) {
  57. return $http.get('/api/items/' + id);
  58. }
  59. }
  60. });
  61.  
  62. .controller('ItemElementsController', function($scope, $routeParams, $location, $http, ItemElements, isEmptyObjectFilter) {
  63. if ($routeParams.itemID !== undefinned){
  64. ItemElements.getOne($routeParams.itemID)
  65. // if successful getByID, call our function to get the Item data
  66. .success(function(data) {
  67. // assign our Item
  68. $scope.item = data;
  69. // for use with a parameter in appRoutes.js using itemID as the variable
  70. $scope.itemID = $routeParams.itemID;
  71. })
  72. .error(function(data) {
  73. console.log('Error: ' + data);
  74. });
  75. }
  76. // GET by ID ==================================================================
  77. // get an Item after clicking it
  78. $scope.getItem = function(id) {
  79. $location.path('/' + id);
  80. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement