Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dynamic pages for each ITEM, on ng-click
- // from $routeParams.itemID in Ctrl
- .when('/:itemID', {
- templateUrl: 'views/item.html',
- controller: 'ItemElementsController'
- })
- app.get('/api/items/:item_id', function(req, res) {
- // use mongoose to get the one emotion from the database
- Item.findById({
- _id : req.params.item_id
- },
- function(err, item) {
- // if there is an error retrieving, send the error. nothing after res.send(err) will execute
- if (err) {
- res.json({ error: err });
- } else {
- res.json(item); // return the item in JSON format
- }
- });
- });
- angular.module('ItemElementsCtrl', [])
- // inject the Item service.factory into our controller
- .controller('ItemElementsController', function($scope, $routeParams, $location, $http, ItemElements, isEmptyObjectFilter) {
- // GET by ID ==================================================================
- // get an Item after clicking it
- $scope.getItem = function(id) {
- ItemElements.getOne(id)
- // if successful getByID, call our function to get the Item data
- .success(function(data) {
- // assign our Item
- $scope.item = data;
- // for use with a parameter in appRoutes.js using itemID as the variable
- $scope.itemID = $routeParams.itemID;
- // redirect
- $location.path('/' + $routeParams.itemID);
- })
- .error(function(data) {
- console.log('Error: ' + data);
- });
- };
- });
- angular.module('ItemElementsService', [])
- // super simple service
- // each function returns a promise object
- .factory('ItemElements', function($http) {
- return {
- getOne : function(id) {
- return $http.get('/api/items/' + id);
- }
- }
- });
- .controller('ItemElementsController', function($scope, $routeParams, $location, $http, ItemElements, isEmptyObjectFilter) {
- if ($routeParams.itemID !== undefinned){
- ItemElements.getOne($routeParams.itemID)
- // if successful getByID, call our function to get the Item data
- .success(function(data) {
- // assign our Item
- $scope.item = data;
- // for use with a parameter in appRoutes.js using itemID as the variable
- $scope.itemID = $routeParams.itemID;
- })
- .error(function(data) {
- console.log('Error: ' + data);
- });
- }
- // GET by ID ==================================================================
- // get an Item after clicking it
- $scope.getItem = function(id) {
- $location.path('/' + id);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement