Priya
Priya

Reputation: 1481

angularjs: Customizing resource

I am following https://github.com/StarterSquad/startersquad.github.com/tree/master/examples/angularjs-requirejs-2 folder structure in my app

inside services i have added following code

 define(['./module'], function(services) {
'use strict';
services.factory('CreditCard', ['$http', function($http) {

function CreditCardFactory() {

    function parseMessage(message) {
        if (message.response) {
            return message.response;
        }
    }

    function CreditCard(value) {
        angular.copy(value || {}, this);
    }

    CreditCard.$get = function(id) {
        var value = this instanceof CreditCard ? this : new CreditCard();
        $http({
            method: 'GET',
            url: '/creditcards/' + id
        }).then(function(response) {
            var data = response.data;
            if (data) {
                angular.copy(parseMessage(data), value);
            }
        });
        return value;
    };

    CreditCard.prototype.$get = function(id) {
        CreditCard.$get.call(this, id);
    };

    return CreditCard;

  }

return CreditCardFactory;

}]);


});

i have followed this question and added above code in factory

Angularjs - customizing $resource

in this question app.controller('CreditCardCtrl', function($scope, CreditCard) { $scope.creditCard = CreditCard().get(3); }); CreditCard is added without adding dependency as we add defualt angular services like $scope and $http.

if i dont add dependency controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources',function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){ then it gives undefined and if i add it in my controller as dependency and then try to call get function then response is not returned

     controllers.controller('myListCtrl',    ['Phone','Phone1','loginForm','$scope','$http','CreditCard',function(Phone,Phone1,loginForm,userSrv,$scope,$http,user_resources,CreditCard){

Upvotes: 0

Views: 63

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37520

'CreditCard' isn't declared as a dependency in this example, therefore it is not being injected and is undefined...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources',
    function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){

To fix this, add 'CreditCard'...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources','CreditCard',
    function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){

In the second example, some dependencies are missing, causing the others to be in the wrong order in comparison to the function arguments...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','CreditCard',
    function(Phone,Phone1,loginForm,userSrv,$scope,$http,user_resources,CreditCard){

Add the missing dependencies in the right order...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','userSrv','$scope','$http','user_resources','CreditCard',
    function(Phone,Phone1,loginForm,userSrv,$scope,$http,user_resources,CreditCard){

Upvotes: 1

Related Questions