[英]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
我正在使用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;
}
};
}
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.
在工厂中,方法checkRecordType具有检索函数,该函数是javascript远程调用,并且该函数具有对loadrecordtype的回调。
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;
}
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. 似乎您以错误的方式返回了工厂对象。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.