Answer the question
In order to leave comments, you need to log in
Have I compiled the server interaction class correctly?
They hired a junior (th), gave the task to write a class for interacting with the server, while pulling out JSON to us and immediately presenting it as a JS object
The project itself works on Materialize (+ jQuery) + Angular
Therefore, I did this: on jQuery
/*
// инициализация объекта
// service = new HttpServer(@address);
var service = new HttpServer("http://192.168.137.57/blackbox/service.svc/");
// запрос на вход в систему
service.login(@login, @password).then(foo);
// получение списка типов процессов доступных пользователю для запуска
service.processtypes().then(foo);
// создание процесса
service.processcreate().then(foo);
// получение моих процессов
service.myprocessesget().then(foo);
// получение шагов процесса
service.myprocessstepsget().then(foo);
*/
var HttpServer = function(adress){
"use private";
var host = adress;
function sendAjax(url, callback){
$.ajax({
url: host + url,
dataType: 'JSON',
jsonpCallback: 'callback',
type: 'GET',
success: function( data ) {
callback(data);
}
});
}
"use public";
this.login = function(login, pass){
this.then = function(callback){
sendAjax(("login?login=" + login + "&password=" + pass), callback);
}; return this;
}
this.processtypes = function(){
this.then = function(callback){
sendAjax(("processtypes/get"), callback);
}; return this;
}
this.processcreate = function(id){
this.then = function(callback){
sendAjax(("process/create/" + id), callback);
}; return this;
}
this.myprocessesget = function(id){
this.then = function(callback){
sendAjax(("processes/get"), callback);
}; return this;
}
this.myprocessstepsget = function(id){
this.then = function(callback){
sendAjax(("processes/" + id + "/processsteps/get"), callback);
}; return this;
}
};
Answer the question
In order to leave comments, you need to log in
You have all requests in GET, although the same /process/create should be in POST for good. The sendAjax method is very limited - you can't pass request parameters there. I would not single out a separate call to the server in sendAjax, but would use $http.get or $http.post in each method - this will turn out to be more flexible, and there is as much code, if not less.
Basically, this is how I did it:
/*
// инициализация объекта
var server = new ClientServer(@address);
server = new ClientServer("http://192.168.137.57/blackbox/service.svc/")
// запрос на вход в систему
server.login(@login, @password, function(req){
$http.get(req).then(foo);
});
// получение списка типов процессов доступных пользователю для запуска
server.processtypes(function(req){
$http.get(req).then(foo);
});
// получение моих процессов
server.myprocessesget(function(req){
$http.get(req).then(foo);
});
// создание процесса
server.processcreate(@process_id, function(req){
$http.get(req).then(foo);
});
// получение шагов процесса
server.myprocessstepsget(@process_id, function(req){
$http.get(req).then(foo);
});
*/
var ClientServer = function(base_url){
"use private";
var url = base_url;
"use public";
this.login = function(login, password, callback){
callback(url + "login?login=" + login + "&password=" + password);
}
this.processtypes = function(callback){
callback(url + "processtypes/get");
}
this.myprocessesget = function(callback){
callback(url + "processes/get");
}
this.processcreate = function(id, callback){
callback(url + "process/create/" + id);
}
this.myprocessstepsget = function(id, callback){
callback(url + "processes/" + id + "/processsteps/get");
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question