我在使用wb.OnDocumentComplete := yourClass.DocCopmlete;
的服务中有一项功能。
在此插件的successc回调中,如何调用此服务的其他方法,或如何操作此服务中的变量?
以下是我正在使用的服务:
$cordovaFileTransfer
我想在成功回调中调用.factory("videoService", function($http, $cordovaCapture, $ionicPlatform, $cordovaFileTransfer, $q, $localstorage, SERVER, USER, UTILS){
var obj = {
// videos list
video_data : {},
// intermediate temp object to store currently being uploaded video data
temp_video_data : {},
// launches camera, uploads video to server on save
captureVideo : function(){
var defer = $q.defer();
// set videCapture options
var videoCapture_options = { limit: 1, duration: 5 };
// set upload meta-data
var user_data = USER.getUserDetails();
var d = String(new Date().toJSON().slice(0,10));
var file_name = "yoro:" + String(user_data["username"]) + ":" + d + ".mp4";
user_data["date"] = d;
user_data["file_name"] = file_name;
console.log("user_data - >");
console.log(user_data);
// launch camera, saving and uploading done in success callback
$cordovaCapture.captureVideo(videoCapture_options)
.then(function(videoData) {
// get local filesystem path of captured video
var url = videoData[0].fullPath;
obj.temp_video_data["url"] = url;
// initiate video uploading to server
document.addEventListener('deviceready', function(){
// set options for uploading
var trustAllHosts = true;
var options_ft = {
fileKey: "yoro_vid",
fileName: file_name,
chunkedMode: false,
mimeType: "video/mp4",
params: user_data,
};
$cordovaFileTransfer.upload(SERVER.upload_video, url, options_ft, trustAllHosts)
.then(
// success callback
function(result, USER) {
console.log("cordovaFileTransfer returned in service ->");
console.log(JSON.stringify(result));
if (result.responseCode===200){
var data = result.response;
console.log(JSON.stringify(data));
// update can_upload flag
//USER.can_upload_flag = result.response.can_upload;
// fetch guid and (pythonic)created_at of uploaded video
var video_data = result.response.video_data;
console.log(JSON.stringify(video_data));
//video_data["local_path"] = videoService.temp_video_data["url"];
// store video information in service object
//obj.video_data.push(video_data);
// store information in localstorage
setVideoDataLocalStorage(video_data);
defer.resolve(true);
}
},
// error callback
function(err) {
defer.resolve(false);
},
// progress
function (progress) {
//alert(progress);
}
);
}, false);
});
return defer.promise;
},
// gets video history : gets all videos' guids and created_at(s)
getVideosHistory : function(){
var defer = $q.defer();
var post_data = {
"username" : USER.user_data["username"],
"passkey" : USER.user_data["passkey"],
"guid" : USER.user_data["guid"],
};
$http.post(SERVER.get_user_history, post_data)
.success(function(data, status, headers, config){
if (status===200){
var number = parseInt(data["videos_number"]);
console.log("Video data obtained -> " + String(number));
console.log(data);
if(number===0){
defer.resolve("empty");
}
else{
obj.video_data = data["videos"];
defer.resolve(true);
}
}
})
.error(function(data, status, headers, config){
console.log("Error in getting video history.")
defer.resolve(false);
});
return defer.promise;
},
// video data in local storage is a list of dictionaries corresponding to a video
setVideoDataLocalStorage : function(video_data){
console.log("in setVideoDataLocalStorage");
for (var k in video_data)
var vid_guid = k;
var local_storage_video_data = $localstorage.getObject("video_data");
local_storage_video_data.push({var_guid : video_data});
console.log("out setVideoDataLocalStorage");
console.log(JSON.stringify($localstorage.getObject("video_data")));
},
}; // end of obj
return obj;
})
方法。
初学者,请帮助!
答案 0 :(得分:1)
有几种方法可以做到,但我发现最简单的方法是将服务对象作为局部变量声明。然后,我只是传递对下面的函数声明的引用,而不是定义对象本身内的所有方法。然后,您可以在服务的任何其他位置轻松访问服务中的任何方法或属性。
app.factory('videoService', function($http, $cordovaCapture, $ionicPlatform, $cordovaFileTransfer, $q, $localstorage, SERVER, USER, UTILS){
// declare or service object as a local variable
// so we can easily access it's properties
// if we need to elsewhere in the service
var videoService = {
videoData : {},
tempVideoData : 'foo',
captureVideo : captureVideo,
getVideosHistory : getVideosHistory,
setVideoDataLocalStorage : setVideoDataLocalStorage
};
// return our service..
return videoService;
//////////////////////////////////////////////////
// implement our service methods...
function captureVideo(){
// ...
$cordovaFileTransfer
.upload(/* args ... */)
.then(function(){
// can easily access other methods on our videoService
videoService.setVideoDataLocalStorage();
});
}
function getVideosHistory(){
// ..implement function here
}
function setVideoDataLocalStorage(videoData){
// ..implement function here
}
});