Tôi tự tạo cấu trúc. Nhưng nó chắc chắn có thể được cải thiện.
Tôi đã cố gắng làm cho tệp main.js của mình trở nên đơn giản. Tôi chỉ thêm các tên hàm sẽ được gọi bên ngoài mã đám mây.
// Include all of the modules
var module1 = require('cloud/folder1/file1.js');
var module2 = require('cloud/folder1/file2.js');
var module3 = require('cloud/folder2/file1.js');
var backgroundjob = require('cloud/backgroundjob/background.js');
Parse.Cloud.job("startBackgroundJob", backgroundjob.startBackgroundJob);
Parse.Cloud.define("do_this_stuff", module1.thisfunction);
Parse.Cloud.define("do_this_stuff2", module1.notthisfunction);
Parse.Cloud.define("do_that_stuff", module2.thatfunction);
Parse.Cloud.define("do_dat_stuff", module3.datfunction);
Trong tệp1.js tôi đã viết chức năng như sau.
// Include libraries
var utils = require("cloud/utils/utils.js");
var _ = require('underscore');
// Export Modules
module.exports = {
thisfunction: function (request, response) {
addComment(request, response);
},
thatfunction: function (request, response) {
getComment(request, response);
},
};
function addComment(request, response) {
// write your code here
var stuff = utils.callThisFunction(param); // This is the usage of another function in another file
response.success("Comment added"); // or error but do not forget this
}
function getComment(request, response) {
// write your code here
response.success("Got Comment"); // or error but do not forget this
}
Tôi đã xuất mô-đun như được hiển thị vì nó làm cho mã dễ đọc hơn. Tôi chỉ có thể nhìn vào đầu mã và xem những chức năng nào tôi có thể sử dụng từ tệp này. Bạn có thể sử dụng docs export style.
exports.addComment = function(request, response) {
// your code
response.success();
}
Điều này rất hữu ích! Tôi đã đi một bước xa hơn và làm như sau: '' ' var CloudFunctions = require ('./ source/cloud-functions.js'); _.each (_. Phím (CloudFunctions), chức năng (functionName) { Parse.Cloud.define (functionName, CloudFunctions [functionName]); }); '' ' Bằng cách này bạn không cần phải xác định từng loại. Hoạt động cho Công việc, lưu/xóa vv quá –