M.S
M.S

Reputation: 41

Transfer data from factory to controller

Somehow I am not able to get my data from the factory method to the controller. I am using javascript remoting and factory is given below

function userRecordFetchFactory($rootScope) {
        var custRec = {};   
            return {                                        
                checkRecordType : function(urlObject) {
                 function loadRecordType(err, records, event) {
                        if (err) {
                            displayReadingInformationErrorView();
                        } else if (records != null && records.length == 0) {
                            displayReadingInformationErrorView();
                        } else {
                             custRec = {
                                Name : records[0].get('Name'),
                                lat : records[0].get('Latitude__c'),
                                lon : records[0].get('Longitude__c'),
                                SiteStreet : records[0]
                                        .get('SiteStreet__c'),
                                SiteCity : records[0].get('SiteCity__c'),
                                SiteCountryCode : records[0]
                                        .get('SiteCountryCode__c'),
                                SitePostalCode : records[0]
                                        .get('SitePostalCode__c'),
                                AddressID : records[0].get('AddressID__c'),
                                loaded : true
                            };

                        }
                    }
                    if (urlObject && urlObject.aid
                            && urlObject.aid.startsWith(accPrefix)) {
                        objModel = new RemoteObjectModel.Account();
                    }
                    if (urlObject && urlObject.aid
                            && urlObject.aid.startsWith(leadPrefix)) {
                        objModel = new RemoteObjectModel.Lead();
                    }

                    if (objModel) {
                        objModel.retrieve({
                            where : {
                                Id : {
                                    eq : urlObject.aid
                                }
                            }
                        }, loadRecordType);
                    }

                    return custRec;
                }
            };
        }

and my controller is given below to access the data

function LocatorInitController($document, $scope,
                userRecordFetchFactory) {
                console.log("inside the controller"+urlParams);
                $scope.CustomerSite = {};
                userRecordFetchFactory.checkRecordType(urlParams)
                .then(function successData(data){
                $scope.CustomerSite = data.data;
                execGeoCoding();
                });

I get an error cannot read property success of undefined. In the factory the method checkRecordType has a retrieve function which is a javascript remoting call and that finction has a callback to loadrecordtype.

Upvotes: 0

Views: 53

Answers (3)

Rajiv
Rajiv

Reputation: 457

function userRecordFetchFactory($rootScope) {
var custRec = {};

    custRec.checkRecordType = function (urlObject) {
        function loadRecordType(err, records, event) {
            if (err) {
                displayReadingInformationErrorView();
            } else if (records != null && records.length == 0) {
                displayReadingInformationErrorView();
            } else {
                custRec = {
                    Name: records[0].get('Name'),
                    lat: records[0].get('Latitude__c'),
                    lon: records[0].get('Longitude__c'),
                    SiteStreet: records[0]
                        .get('SiteStreet__c'),
                    SiteCity: records[0].get('SiteCity__c'),
                    SiteCountryCode: records[0]
                        .get('SiteCountryCode__c'),
                    SitePostalCode: records[0]
                        .get('SitePostalCode__c'),
                    AddressID: records[0].get('AddressID__c'),
                    loaded: true
                };

            }
        }
        if (urlObject && urlObject.aid
            && urlObject.aid.startsWith(accPrefix)) {
            objModel = new RemoteObjectModel.Account();
        }
        if (urlObject && urlObject.aid
            && urlObject.aid.startsWith(leadPrefix)) {
            objModel = new RemoteObjectModel.Lead();
        }

        if (objModel) {
            objModel.retrieve({
                where: {
                    Id: {
                        eq: urlObject.aid
                    }
                }
            }, 
return custRec;
}

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

Suggest you write your factory in a simpler way to read it. All your nesting is causing you not to see the whole thing easily

Put all the accessible members up top

// pass functions as references to object properties
// can easily see the whole factory object at top of the file
var custRec = {
  checkRecordType : checkRecordType,
  subscribe       : subscribe    
};

return custRec;


// move function declarations to the bottom and out of the way

function checkRecordType(){
    /// do stuff
    return stuff;
}

function loadRecordType(err, records, event) {
   /// do stuff
    return stuff;
}

function subscribe(scope, callback){
   /// do stuff
    return stuff;
}

See John Papa Angular STyle Guide

Upvotes: 1

nikjohn
nikjohn

Reputation: 21870

Why don't you try rewriting your factory like so:

function Factory() {
var custRec = {

    subscribe: function(scope, callback) {
        var handler = $rootScope.$on(
            'fire-event-accountservice-retrieve',
            function(
                event, data) {
                callback(data);
                scope.$apply();
            });
        scope.$on('$destroy', handler);
    },
    checkRecordType: function(urlObject) {
        var custRec;
        if (urlObject.aid == null && urlObject.addr == null) {
            displayCurrentLocation();
        }
        if (urlObject && urlObject.aid &&
            urlObject.aid.startsWith(accPrefix)) {
            objModel = new RemoteObjectModel.Account();
        }
        if (urlObject && urlObject.aid &&
            urlObject.aid.startsWith(leadPrefix)) {
            objModel = new RemoteObjectModel.Lead();
        }
        if (objModel == null && urlObject.aid != null && urlObject.addr == null) {
            displayReadingInformationErrorView();
        }
        if (objModel) {
            objModel.retrieve({
                where: {
                    Id: {
                        eq: urlObject.aid
                    }
                }
            }, loadRecordType);
        } else if ((urlObject.addr != null || urlObject.addr != '') && (typeof urlObject.addr != "undefined")) {
            displayLocationBasedOnAddress(urlObject.addr);
        }

        function loadRecordType(err, records, event) {
            if (err) {
                displayReadingInformationErrorView();
            } else if (records != null && records.length == 0) {
                displayReadingInformationErrorView();
            } else {
                custRec = {
                    Name: records[0].get('Name'),
                    lat: records[0].get('Latitude__c'),
                    lon: records[0].get('Longitude__c'),
                    SiteStreet: records[0]
                        .get('SiteStreet__c'),
                    SiteCity: records[0].get('SiteCity__c'),
                    SiteCountryCode: records[0]
                        .get('SiteCountryCode__c'),
                    SitePostalCode: records[0]
                        .get('SitePostalCode__c'),
                    AddressID: records[0].get('AddressID__c'),
                    loaded: true
                };

                /*  $rootScope.$emit(
                            'fire-event-accountservice-retrieve',
                            custRec); */
            }
        }

    }
}
return custRec;
}

Looks like you're returning your factory object the wrong way.

Upvotes: 0

Related Questions