Skip to content

Commit

Permalink
Refactor the date transformations to a service
Browse files Browse the repository at this point in the history
  • Loading branch information
gzsombor committed May 28, 2015
1 parent 7b8ccd7 commit 3d78486
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ JhipsterGenerator.prototype.app = function app() {
this.template(webappDir + '/scripts/components/util/_base64.service.js', webappDir + 'scripts/components/util/base64.service.js', this, {});
this.template(webappDir + '/scripts/components/util/_parse-links.service.js', webappDir + 'scripts/components/util/parse-links.service.js', this, {});
this.template(webappDir + '/scripts/components/util/_truncate.filter.js', webappDir + 'scripts/components/util/truncate.filter.js', this, {});
this.template(webappDir + '/scripts/components/util/_dateutil.service.js', webappDir + 'scripts/components/util/dateutil.service.js', this, {});

// Client App
this.template(webappDir + '/scripts/app/account/_account.js', webappDir + 'scripts/app/account/account.js', this, {});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

angular.module('<%=angularAppName%>')
.service('DateUtils', function () {
this.convertLocaleDateToServer = function(date) {
if (date) {
var utcDate = new Date();
utcDate.setUTCDate(date.getDate());
utcDate.setUTCMonth(date.getMonth());
utcDate.setUTCFullYear(date.getFullYear());
return utcDate;
} else {
return null;
}
};
this.convertLocaleDateFromServer = function(date) {
if (date) {
var dateString = date.split("-");
return new Date(dateString[0], dateString[1] - 1, dateString[2]);
}
return null;
};
this.convertDateTimeFromServer = function(date) {
if (date) {
return new Date(date);
} else {
return null;
}
}
});
21 changes: 5 additions & 16 deletions entity/templates/src/main/webapp/components/_entity-service.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
'use strict';

angular.module('<%=angularAppName%>')
.factory('<%= entityClass %>', function ($resource) {
.factory('<%= entityClass %>', function ($resource, DateUtils) {
return $resource('api/<%= entityInstance %>s/:id', {}, {
'query': { method: 'GET', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
data = angular.fromJson(data);<% for (fieldId in fields) { if (fields[fieldId].fieldType == 'LocalDate') { %>
if (data.<%=fields[fieldId].fieldName%> != null){
var <%=fields[fieldId].fieldName%>From = data.<%=fields[fieldId].fieldName%>.split("-");
data.<%=fields[fieldId].fieldName%> = new Date(new Date(<%=fields[fieldId].fieldName%>From[0], <%=fields[fieldId].fieldName%>From[1] - 1, <%=fields[fieldId].fieldName%>From[2]));
}<% }if (fields[fieldId].fieldType == 'DateTime') { %>
if (data.<%=fields[fieldId].fieldName%> != null) data.<%=fields[fieldId].fieldName%> = new Date(data.<%=fields[fieldId].fieldName%>);<% } }%>
data.<%=fields[fieldId].fieldName%> = DateUtils.convertLocaleDateFromServer(data.<%=fields[fieldId].fieldName%>);<% }if (fields[fieldId].fieldType == 'DateTime') { %>
data.<%=fields[fieldId].fieldName%> = DateUtils.convertDateTimeFromServer(data.<%=fields[fieldId].fieldName%>);<% } }%>
return data;
}
},<% if (fieldsContainLocalDate == true) { %>
'update': {
method: 'PUT',
transformRequest: function (data) {<% for (fieldId in fields) { if (fields[fieldId].fieldType == 'LocalDate') { %>
var <%=fields[fieldId].fieldName%> = new Date();
<%=fields[fieldId].fieldName%>.setUTCDate(data.<%=fields[fieldId].fieldName%>.getDate());
<%=fields[fieldId].fieldName%>.setUTCMonth(data.<%=fields[fieldId].fieldName%>.getMonth());
<%=fields[fieldId].fieldName%>.setUTCFullYear(data.<%=fields[fieldId].fieldName%>.getFullYear());
data.<%=fields[fieldId].fieldName%> = <%=fields[fieldId].fieldName%>;<% } }%>
data.<%=fields[fieldId].fieldName%> = DateUtils.convertLocaleDateToServer(data.<%=fields[fieldId].fieldName%>);<% } }%>
return angular.toJson(data);
}
},
'save': {
method: 'POST',
transformRequest: function (data) {<% for (fieldId in fields) { if (fields[fieldId].fieldType == 'LocalDate') { %>
var <%=fields[fieldId].fieldName%> = new Date();
<%=fields[fieldId].fieldName%>.setUTCDate(data.<%=fields[fieldId].fieldName%>.getDate());
<%=fields[fieldId].fieldName%>.setUTCMonth(data.<%=fields[fieldId].fieldName%>.getMonth());
<%=fields[fieldId].fieldName%>.setUTCFullYear(data.<%=fields[fieldId].fieldName%>.getFullYear());
data.<%=fields[fieldId].fieldName%> = <%=fields[fieldId].fieldName%>;<% } }%>
data.<%=fields[fieldId].fieldName%> = DateUtils.convertLocaleDateToServer(data.<%=fields[fieldId].fieldName%>);<% } }%>
return angular.toJson(data);
}
}<% } else { %>
Expand Down

1 comment on commit 3d78486

@deepu105
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the js file is not injected into index html causing issue jhipster#1549

Please sign in to comment.