2016-10-03 60 views
0

不知何故,我無法將我的數據從工廠方法傳輸到控制器。我使用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的回調。

回答

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

你好像退回工廠對象走錯了路。

+0

好吧,我已經重構代碼,現在把。那麼我的錯誤表示userRecordFetchFactory.checkRecordType(...)。那麼這不是一個函數 –

+0

。這意味着你的客戶正在期待一個承諾。僅僅從checkRecordType的簡要概述,它看起來不像是一個承諾正在返回。 – westandy

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

+0

loadRecordType不能超出checkRecordType。另外,您可以刪除訂閱功能,因爲它是沒有必要的。我仍然無法將數據傳送給控制器。 –

+0

但爲什麼不呢?此外,您絕不應將範圍傳遞到服務 – charlietfl

+0

loadRecordType是一種使用JavaScript遠程處理獲取salesforce中的數據的方法。因此,如果您查看調用loadRecordType的checkRecordType中的Retrieve函數。因此它不能在checkrecordtype –

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; 
} 
相關問題