amejiarosario / meanshop

🛒 Building an e-commerce application with the MEAN stack
http://meanshop.com
GNU General Public License v2.0
149 stars 104 forks source link

why you use objects with only one field, when you could use a simple data type? #30

Closed dportabella closed 8 years ago

dportabella commented 8 years ago

Is there any reason why in chapter 2, the get function of the product service takes an object params (with only one field id) as a parameter, instead of just a simple parameter productId?

you have:

https://github.com/amejiarosario/meanshop/blob/ch2/client/app/products/products.service.js
      get: function(params){
          if(product._id == params.id){

https://github.com/amejiarosario/meanshop/blob/ch2/client/app/products/products.controller.js
    $scope.product = Product.get({id: $stateParams.id});

why not simply?

https://github.com/amejiarosario/meanshop/blob/ch2/client/app/products/products.service.js
      get: function(productId){
          if(product._id == productId){

https://github.com/amejiarosario/meanshop/blob/ch2/client/app/products/products.controller.js
    $scope.product = Product.get($stateParams.id);
amejiarosario commented 8 years ago

Good question. In chapter 2, it's ok either way. However, in chapter 5, we start using $resource and we need to specify the name of the parameter that matches a route, like this:

// https://github.com/amejiarosario/meanshop/blob/ch5/client/app/products/products.service.js

angular.module('meanshopApp')
  .factory('Product', function ($resource) {
    return $resource('/api/products/:id', null, {
      'update': { method: 'PUT'}
    });
  });

Notice that we are using a placeholder called :id. When we use the controller we need to match it as follows:

// https://github.com/amejiarosario/meanshop/blob/ch5/client/app/products/products.controller.js

$scope.product = Product.get({id: $stateParams.id});
dportabella commented 8 years ago

I see. thanks.