0

We have two angularjs services. The first service is called initially to retrieve data, then the second service is called. When adding a breakpoint on 'self' in the second service, its always populated with 'self' from the first service. ie contains 'isloadinggrid etc'. Is there a reason for this? i had assumed that self in each would have been unique to the service object.

(function () {
    'use strict';
angular.module('IPortal.Angular.Components.List.Shared.List.Factory', [])
        .service('listfactory', ['$http', 'loggingService', 'listApiLink', 'listFormRenderService', 'listGridRenderService', function ($http, loggingService, listApiLink, listFormRenderService, listGridRenderService) {
            self.Model = {
            IsLoadingGrid: false,
            IsLoadingForm: false,
            TotalColumns: 0,
            TotalRows: 0,
            List:[],
            ListGrid: [],
            ListForm: [],
            ListFormTemplateURL: '',
            ListGridTemplateURL: '',
            BaseDate: new Date(),
            PendingUpdates : [],
        };




        return {
            GetListbyId: function (id, listType) {
                self.GetListbyId(id, listType);
                return self.Model;
            },
            GetListDetailbyId: function (id, listType) {
                self.GetListDetailbyId(id, listType);
                return self.Model;
            },
            SaveAnswer: function (pendingQuestion) {
                var pendingAdd = true;

                if (self.Model.PendingUpdates.length > 0) {
                    self.Model.PendingUpdates = self.Model.PendingUpdates.filter(function (pendingValue) {
                        if (pendingValue.id === pendingQuestion.id) {
                                pendingAdd = true
                                return false
                        }
                        return true;
                    });
                }

                if (pendingAdd) {
                    self.Model.PendingUpdates.push(pendingQuestion)
                }
            },
            Save: function () {
                var y = self.Model.PendingUpdates;
            }
        };
        }]);
})();

2.

(function () {
    'use strict';

    angular.module('IPortal.Angular.Components.List.ListForm.InputDirectives.ClinicalOrderStatus.factory', [])
        .service('clinicalorderstatusfactory', ['$http', 'loggingService', function ($http, loggingService) {

            self.Model = {
                primaryKey: 0,
                status : '', 
                statusCode : '',
            };

            self.UpdataStatus = function (primaryKey, patientId, status, reason, accountId) {

            };

            self.RenderForm = function () {
                self.Model.ListFormTemplateURL = listFormRenderService.Render(self.Model.ListForm);
                self.Model.FormTemplateIsRendered = true;
            }


            return {
                UpdateStatus: function (primaryKey, patientId, status, reason, accountId) {
                    self.UpdataStatus(primaryKey, patientId, status, reason, accountId);
                    return self.Model;
                },
                Data: self.Model,
            };
        }]);

})();
georgeawg
  • 46,994
  • 13
  • 63
  • 85
Simon
  • 1,270
  • 4
  • 18
  • 41
  • 2
    From where "self" is coming? – robert Mar 09 '19 at 13:48
  • @robert good eyes!, well spotted.. missing var self = this; – Simon Mar 09 '19 at 13:57
  • I managed to put together a test app with these two services. In fact both of them should be registered as a factory. Anyway. The issue is somewhere else. In both case self points to a proper empty object at the beginning. Can you provide more code? How do you actually use these services. – robert Mar 09 '19 at 14:33
  • All services are singletons. – georgeawg Mar 09 '19 at 17:58

0 Answers0