将数据从工厂传输到控制器

时间:2016-10-03 18:19:29

标签: javascript angularjs

不知何故,我无法将我的数据从工厂方法获取到控制器。我正在使用javascript远程处理,工厂在下面给出

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;
                }
            };
        }

我的控制器在下面给出了访问数据

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();
                });

我得到一个错误,无法读取未定义的属性成功。在工厂中,方法checkRecordType有一个检索函数,它是一个javascript远程调用,并且该函数有一个回调到loadrecordtype。

3 个答案:

答案 0 :(得分:1)

建议您以更简单的方式编写工厂以进行阅读。你所有的筑巢都让你不能轻易看到整件事

将所有可访问的成员放在首位

// 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;
}

请参阅John Papa Angular STyle Guide

答案 1 :(得分:0)

为什么不尝试重写你的工厂:

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;
}

看起来你正以错误的方式返回工厂对象。

答案 2 :(得分:0)

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;
}