Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit a4f167a

Browse files
committed
Bugfixes
1 parent 57cec3b commit a4f167a

File tree

7 files changed

+58
-36
lines changed

7 files changed

+58
-36
lines changed

grails-app/commands/angular2/scaffolding/NgGenerateAllCommand.groovy

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,35 @@ class NgGenerateAllCommand implements GrailsApplicationCommand {
8585
List<String> componentProperties = []
8686
List<String> componentImports = []
8787
Map<String, String> domainProperties = [:]
88-
List<String> domainConstructorInitializingStatements = []
88+
Map<String, List<String>> domainConstructorInitializingStatements = [:]
8989
List<String> domainImports = []
9090

9191
domainModelService.getInputProperties(domainClass).each { DomainProperty property ->
9292
PersistentProperty prop = property.persistentProperty
9393
domainProperties.put(property.name, 'any')
9494
if (prop instanceof Association) {
95-
associatedProperties.add(property)
9695
String type = property.associatedType.simpleName
9796
String name = GrailsNameUtils.getPropertyName(type)
97+
associatedProperties.add(property)
9898

99-
constructorArguments.add("private ${name}Service: ${type}Service")
100-
initializingStatements.add("this.${name}Service.list().subscribe((${name}List: ${type}[]) => { this.${name}List = ${name}List; });")
101-
componentProperties.add("${name}List: ${type}[];")
102-
componentImports.add("import { ${type}Service } from '../${name}/${name}.service';")
103-
componentImports.add("import { ${type} } from '../${name}/${name}';")
99+
if (!prop.bidirectional) {
100+
constructorArguments.add("private ${name}Service: ${type}Service")
101+
initializingStatements.add("this.${name}Service.list().subscribe((${name}List: ${type}[]) => { this.${name}List = ${name}List; });")
102+
componentProperties.add("${name}List: ${type}[];")
103+
componentImports.add("import { ${type}Service } from '../${name}/${name}.service';")
104+
componentImports.add("import { ${type} } from '../${name}/${name}';")
105+
}
104106
domainImports.add("import { ${type} } from '../${name}/${name}';")
107+
108+
String initializingStatement
105109
if (prop instanceof ToMany) {
106110
domainProperties.put(property.name, "$type[]")
107-
domainConstructorInitializingStatements.add("this.${property.name} = object['${property.name}'].map((obj: any) => { return new ${type}(obj); });")
111+
initializingStatement = "this.${property.name} = object['${property.name}'].map((obj: any) => { return new ${type}(obj); });"
108112
} else {
109113
domainProperties.put(property.name, type)
110-
domainConstructorInitializingStatements.add("this.${property.name} = new ${type}(object['${property.name}']);")
114+
initializingStatement = "this.${property.name} = new ${type}(object['${property.name}']);"
111115
}
112-
domainConstructorInitializingStatements.add("delete object['${property.name}'];")
116+
domainConstructorInitializingStatements.put(property.name, [initializingStatement, "delete object['${property.name}'];"])
113117
} else {
114118
if (fileInputRenderer.supports(property)) {
115119
hasFileProperty = true
@@ -145,7 +149,7 @@ class NgGenerateAllCommand implements GrailsApplicationCommand {
145149
File moduleFile = file("${baseDir}/${module.propertyName}/${module.propertyName}.module.ts")
146150
render template: template("angular2/javascripts/module.ts"),
147151
destination: moduleFile,
148-
model: module.asMap() << [associatedModule: false],
152+
model: module.asMap() << [associatedModule: false, importService: true],
149153
overwrite: overwrite
150154

151155
render template: template("angular2/javascripts/routing.module.ts"),
@@ -179,25 +183,27 @@ class NgGenerateAllCommand implements GrailsApplicationCommand {
179183
}
180184

181185

182-
associatedProperties.each {
186+
associatedProperties.each { DomainProperty prop ->
183187

184-
Model associatedModel = model(it.associatedType)
188+
Model associatedModel = model(prop.associatedType)
185189

186190
uri = getUri(associatedModel)
187-
188-
render template: template("angular2/javascripts/service.ts"),
189-
destination: file("${baseDir}/${associatedModel.propertyName}/${associatedModel.propertyName}.service.ts"),
190-
model: associatedModel.asMap() << [uri: uri],
191-
overwrite: overwrite
191+
boolean bidirectional = ((Association)prop.persistentProperty).bidirectional
192+
if (!bidirectional) {
193+
render template: template("angular2/javascripts/service.ts"),
194+
destination: file("${baseDir}/${associatedModel.propertyName}/${associatedModel.propertyName}.service.ts"),
195+
model: associatedModel.asMap() << [uri: uri],
196+
overwrite: overwrite
197+
}
192198

193199
render template: template("angular2/javascripts/domain.ts"),
194200
destination: file("${baseDir}/${associatedModel.propertyName}/${associatedModel.propertyName}.ts"),
195-
model: associatedModel.asMap() << [domainProperties: [:], domainConstructorInitializingStatements: [], domainImports: []],
201+
model: associatedModel.asMap() << [domainProperties: [:], domainConstructorInitializingStatements: [:], domainImports: []],
196202
overwrite: overwrite
197203

198204
render template: template("angular2/javascripts/module.ts"),
199205
destination: file("${baseDir}/${associatedModel.propertyName}/${associatedModel.propertyName}.module.ts"),
200-
model: associatedModel.asMap() << [associatedModule: true],
206+
model: associatedModel.asMap() << [associatedModule: true, importService: !bidirectional],
201207
overwrite: overwrite
202208

203209
if (angularModuleEditor.addDependency(moduleFile, associatedModel, '..')) {
@@ -223,5 +229,9 @@ class NgGenerateAllCommand implements GrailsApplicationCommand {
223229
}
224230
}
225231

232+
void renderDomain(PersistentEntity persistentEntity) {
233+
234+
}
235+
226236

227237
}

src/main/groovy/org/grails/plugin/scaffolding/angular2/registry/input/AngularAssociationInputRenderer.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import org.grails.datastore.mapping.model.PersistentProperty
55
import org.grails.datastore.mapping.model.types.Association
66
import org.grails.datastore.mapping.model.types.Basic
77
import org.grails.datastore.mapping.model.types.ToMany
8+
import org.grails.plugin.scaffolding.angular2.registry.output.PropertyHelper
89
import org.grails.scaffolding.model.property.DomainProperty
910
import org.grails.scaffolding.registry.DomainInputRenderer
1011

11-
class AngularAssociationInputRenderer implements DomainInputRenderer {
12+
class AngularAssociationInputRenderer implements DomainInputRenderer, PropertyHelper {
1213

1314
boolean supports(DomainProperty property) {
1415
PersistentProperty persistentProperty = property.persistentProperty
@@ -26,7 +27,7 @@ class AngularAssociationInputRenderer implements DomainInputRenderer {
2627

2728
return { ->
2829
select('', defaultAttributes) {
29-
option("{{${name}.toString()}}", ["*ngFor": "let $name of ${name}List", "[value]": "${name}.id"])
30+
option("{{${name}.toString()}}", ["*ngFor": "let $name of ${name}List", "[value]": "${name}.id", "[selected]": "${buildPropertyPath(property)}?.id == ${name}.id"])
3031
}
3132
}
3233
}

src/main/groovy/org/grails/plugin/scaffolding/angular2/registry/output/AngularToOneOutputRenderer.groovy

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package org.grails.plugin.scaffolding.angular2.registry.output
22

3-
import grails.util.GrailsNameUtils
43
import org.grails.datastore.mapping.model.types.ToOne
54
import org.grails.scaffolding.model.property.DomainProperty
65

7-
/**
8-
* Created by Jim on 5/25/2016.
9-
*/
106
class AngularToOneOutputRenderer extends AngularDomainOutputRenderer {
117

128
@Override

src/main/templates/angular2/javascripts/domain.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ export class ${className} {
66
<%= domainProperties.collect { k,v -> "${k}: ${v};" }.join('\n ') %>
77

88
constructor (object?: any) {
9-
<%= domainConstructorInitializingStatements.join('\n ') %>
10-
for (var prop in object) {
11-
this[prop] = object[prop];
9+
if (object) {
10+
<% domainConstructorInitializingStatements.keySet().each { String key -> %>
11+
if (object.hasOwnProperty('${key}')) {
12+
<%= domainConstructorInitializingStatements.get(key).join('\n ') %>
13+
}
14+
<% } %>
15+
for (var prop in object) {
16+
this[prop] = object[prop];
17+
}
1218
}
19+
1320
}
1421

1522
toString(): string {

src/main/templates/angular2/javascripts/module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {CommonModule} from '@angular/common';
22
import {NgModule} from '@angular/core';
33
import {FormsModule} from '@angular/forms';
4+
<% if (importService) { %>
45
import {${className}Service} from './${propertyName}.service';
6+
<% } %>
57
<% if (!associatedModule) { %>
68
import {${className}RoutingModule} from './${propertyName}-routing.module';
79
import {${className}ShowComponent} from './${propertyName}-show.component';
@@ -19,8 +21,8 @@ import {${className}PersistComponent} from './${propertyName}-persist.component'
1921
FormsModule,<% if (!associatedModule) { %>
2022
${className}RoutingModule<% } %>
2123
],
22-
providers: [
23-
${className}Service
24+
providers: [<% if (importService) { %>
25+
${className}Service<% } %>
2426
]
2527
})
2628
export class ${className}Module {}

src/main/templates/angular2/javascripts/service-file.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,26 @@ export class ${className}Service {
3131
}
3232

3333
save(${propertyName}: ${className}): Observable<${className}> {
34-
let headers = new Headers();
34+
const headers = new Headers();
3535
headers.append("Accept", 'application/json');
3636
headers.delete("Content-Type");
3737
headers.append('X-Requested-With', 'XMLHttpRequest');
3838

39-
let formData: FormData = new FormData();
39+
const formData: FormData = new FormData();
4040

4141
for (let property in ${propertyName}) {
4242
if (${propertyName}.hasOwnProperty(property)) {
43-
formData.append(property, ${propertyName}[property]);
43+
let value;
44+
if (${propertyName}[property] instanceof Date) {
45+
value = ${propertyName}[property].toISOString();
46+
} else {
47+
value = ${propertyName}[property];
48+
}
49+
formData.append(property, value);
4450
}
4551
}
4652

47-
let requestOptions = new RequestOptions({
53+
const requestOptions = new RequestOptions({
4854
headers: headers,
4955
body: formData
5056
});

src/main/templates/angular2/javascripts/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ${className}Service {
3131
}
3232

3333
save(${propertyName}: ${className}): Observable<${className}> {
34-
let requestOptions = new RequestOptions();
34+
const requestOptions = new RequestOptions();
3535
if (${propertyName}.id) {
3636
requestOptions.method = RequestMethod.Put;
3737
requestOptions.url = this.baseUrl + '${uri}/' + ${propertyName}.id;

0 commit comments

Comments
 (0)