Commit 77a2ebe9 authored by Evren Kutar's avatar Evren Kutar

diff implemented on submit of form service

parent 61f021ef
...@@ -16,11 +16,12 @@ var staff = angular.module('zaerp.staff', ['ngRoute', 'schemaForm', 'formService ...@@ -16,11 +16,12 @@ var staff = angular.module('zaerp.staff', ['ngRoute', 'schemaForm', 'formService
* which provide a form with form generator. * which provide a form with form generator.
*/ */
staff.controller('StaffAddCtrl', function ($scope, $http, $log, Generator, $routeParams) { staff.controller('StaffAddCtrl', function ($scope, $http, $log, Generator) {
Generator.get_form('add_staff', '').then(function (d) { Generator.get_form('add_staff', '').then(function (d) {
$scope.schema = d.schema; $scope.schema = d.schema;
$scope.form = d.form; $scope.form = d.form;
$scope.model = d.model ? d.model : {}; $scope.model = d.model ? d.model : {};
$scope.initialModel = angular.copy(d.model);
$scope.form[0].$asyncValidators = Generator.asyncValidators; $scope.form[0].$asyncValidators = Generator.asyncValidators;
$scope.form.push( $scope.form.push(
{ {
...@@ -33,8 +34,7 @@ staff.controller('StaffAddCtrl', function ($scope, $http, $log, Generator, $rout ...@@ -33,8 +34,7 @@ staff.controller('StaffAddCtrl', function ($scope, $http, $log, Generator, $rout
$scope.onSubmit = function (form) { $scope.onSubmit = function (form) {
$scope.$broadcast('schemaFormValidate'); $scope.$broadcast('schemaFormValidate');
if (form.$valid) { if (form.$valid) {
// todo: implement form diff here Generator.submit('add_staff', $scope);
$log.info($scope);
} }
} }
}); });
...@@ -44,6 +44,7 @@ staff.controller('StaffEditCtrl', function ($scope, $http, $log, Generator, $rou ...@@ -44,6 +44,7 @@ staff.controller('StaffEditCtrl', function ($scope, $http, $log, Generator, $rou
$scope.schema = d.schema; $scope.schema = d.schema;
$scope.form = d.form; $scope.form = d.form;
$scope.model = d.model ? d.model : {}; $scope.model = d.model ? d.model : {};
$scope.initialModel = angular.copy(d.model);
$scope.form[0].$asyncValidators = Generator.asyncValidators; $scope.form[0].$asyncValidators = Generator.asyncValidators;
$scope.form.push( $scope.form.push(
{ {
...@@ -56,8 +57,7 @@ staff.controller('StaffEditCtrl', function ($scope, $http, $log, Generator, $rou ...@@ -56,8 +57,7 @@ staff.controller('StaffEditCtrl', function ($scope, $http, $log, Generator, $rou
$scope.onSubmit = function (form) { $scope.onSubmit = function (form) {
$scope.$broadcast('schemaFormValidate'); $scope.$broadcast('schemaFormValidate');
if (form.$valid) { if (form.$valid) {
// todo: implement form diff here Generator.submit('edit_staff', $scope);
$log.info($scope);
} }
} }
}); });
......
...@@ -35,14 +35,12 @@ student.controller('StudentAddEditCtrl', function($scope, $http, $log, Generator ...@@ -35,14 +35,12 @@ student.controller('StudentAddEditCtrl', function($scope, $http, $log, Generator
} }
); );
}); });
$scope.onSubmit = Generator.submit($scope); $scope.onSubmit = function (form) {
//$scope.$broadcast('schemaFormValidate'); $scope.$broadcast('schemaFormValidate');
//if (form.$valid) { if (form.$valid) {
// todo: implement form diff here Generator.submit('add_staff', $scope);
//Generator.submit($scope); }
//$log.info($scope.initialModel, $scope.model); }
//}
//}
}); });
/** /**
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
* (GPLv3). See LICENSE.txt for details. * (GPLv3). See LICENSE.txt for details.
*/ */
var form_generator = angular.module('formService', []); var form_generator = angular.module('formService', ['general']);
form_generator.factory('Generator', function ($http, $q, $log, $timeout, RESTURL) { form_generator.factory('Generator', function ($http, $q, $log, $timeout, RESTURL, FormDiff) {
var generator = {}; var generator = {};
generator.generate = function (modelObject) { generator.generate = function (modelObject) {
return generator.group(modelObject); return generator.group(modelObject);
...@@ -54,14 +54,12 @@ form_generator.factory('Generator', function ($http, $q, $log, $timeout, RESTURL ...@@ -54,14 +54,12 @@ form_generator.factory('Generator', function ($http, $q, $log, $timeout, RESTURL
return deferred.promise; return deferred.promise;
} }
}; };
generator.submit = function ($scope) { generator.submit = function (url, $scope) {
$scope.$broadcast('schemaFormValidate'); var get_diff = FormDiff.get_diff($scope.model,$scope.initialModel);
//if ($scope.form.$valid) { $log.info(get_diff);
// todo: change post url, this is not correct! $http.post(RESTURL.url + url, get_diff).then(function (res) {
$http.post('http://127.0.0.1:3000/api/add_student', $scope.model).then(function (res) {
$log.info(res); $log.info(res);
}); });
//}
}; };
return generator; return generator;
}); });
\ No newline at end of file
...@@ -18,12 +18,14 @@ general.factory('FormDiff', function () { ...@@ -18,12 +18,14 @@ general.factory('FormDiff', function () {
* function to return diff of models of submitted form * function to return diff of models of submitted form
* @type {{}} * @type {{}}
* @params obj1, obj2 * @params obj1, obj2
* @attention: obj1 must be initialModel, obj2 must be model after inputs
* filled
*/ */
var formDiff = {}; var formDiff = {};
formDiff.get_diff = function (obj1, obj2) { formDiff.get_diff = function (obj1, obj2) {
var result = {}; var result = {};
for (key in obj1) { for (key in obj1) {
if (obj2[key] != obj1[key]) result[key] = obj2[key]; if (obj2[key] != obj1[key]) result[key] = obj1[key];
if (typeof obj2[key] == 'array' && typeof obj1[key] == 'array') if (typeof obj2[key] == 'array' && typeof obj1[key] == 'array')
result[key] = arguments.callee(obj1[key], obj2[key]); result[key] = arguments.callee(obj1[key], obj2[key]);
if (typeof obj2[key] == 'object' && typeof obj1[key] == 'object') if (typeof obj2[key] == 'object' && typeof obj1[key] == 'object')
......
...@@ -24,19 +24,36 @@ describe('general module', function () { ...@@ -24,19 +24,36 @@ describe('general module', function () {
]; ];
// test cases - testing for failure // test cases - testing for failure
var different_jsons = [
[
{email: 'test@test.com', id: 2, name: 'travolta'},
{email: 'test1@test.com', id: 2, name: 'john'}
],
[
{id: 2, name: 'travolta'},
{email: 'test1@test.com', id: 2, name: 'john'}
]
];
var different_json = [ var different_json = [
{email: 'test@test.com', id: 2, name: 'travolta'}, {},
{email: 'test1@test.com', id: 2, name: 'john'} {email: 'test1@test.com', id: 2, name: 'john'}
]; ]
var diff = {email: 'test1@test.com', name: 'john'}; var diff = {email: 'test1@test.com', name: 'john'};
var diff2 = {email: 'test1@test.com', id: 2, name: 'john'};
var nodiff = {}; var nodiff = {};
var same = FormDiff.get_diff(same_json[0], same_json[1]); var same = FormDiff.get_diff(same_json[0], same_json[1]);
expect(same).toEqual(nodiff); expect(same).toEqual(nodiff);
var different = FormDiff.get_diff(different_json[0], different_json[1]); for (var json_obj in different_jsons) {
expect(different).toEqual(diff); var different = FormDiff.get_diff(different_jsons[json_obj][1], different_jsons[json_obj][0]);
expect(different).toEqual(diff);
}
var different2 = FormDiff.get_diff(different_json[1], different_json[0]);
expect(different2).toEqual(diff2);
}]) }])
); );
......
...@@ -12,6 +12,14 @@ app.config(['$httpProvider', function ($httpProvider) { ...@@ -12,6 +12,14 @@ app.config(['$httpProvider', function ($httpProvider) {
*/ */
$httpProvider.interceptors.push(function ($q) { $httpProvider.interceptors.push(function ($q) {
return { return {
'request': function(config){
if (config.method == "POST"){
console.log("post request")
} else {
console.log("get request")
}
return config;
},
'response': function (response) { 'response': function (response) {
//Will only be called for HTTP up to 300 //Will only be called for HTTP up to 300
return response; return response;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment