Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions generator/templates/Crud/src/components/Resource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ export default {
let item;

if (this.modelType) {
item = new this.modelType();
item.mapResponse(response.data.data);
item = new this.modelType().mapResponse(response.data.data);
} else {
item = response.data.data;
}
Expand Down Expand Up @@ -239,8 +238,7 @@ export default {
}

if (this.modelType) {
this.updateForm.values = new this.modelType();
this.updateForm.values.mapResponse(selected[0]);
this.updateForm.values = new this.modelType().mapResponse(selected[0]);
} else {
this.updateForm.values = selected[0];
}
Expand Down
61 changes: 58 additions & 3 deletions generator/templates/Default/src/application/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
*
*/
class Model {

/**
* @return {{}};
*/
Expand All @@ -12,10 +11,66 @@ class Model {

/**
* @param data {{}}
* @return {model}
*/
mapResponse(data) {
throw new Error(`${this.constructor.name}::mapResponse unset, keys: ${Object.keys(data)
.join(', ')}`);
return this.initializeObjectFromApi(data, this);
}

/**
* @private
* @param data
* @param assignable
* @return {model}
*/
initializeObjectFromApi(data, assignable) {
Object.keys(assignable)
.forEach(key => {
const value = assignable[key];
const type = typeof value;

if (typeof data[key] === type) {
assignable[key] = data[key];
return;
}

const defaultValue = this.getDefaultValueFromType(type, value);

assignable[key] = data[key] || defaultValue;
});

Object.keys(data)
.forEach(key => {
if (typeof assignable[key] === 'undefined') {
assignable[key] = data[key];
}
});

return assignable;
}

/**
* @private
* @param type
* @param defaultModelValue
* @return {*}
*/
getDefaultValueFromType(type, defaultModelValue) {
let defaultValue;

if (type === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

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

switch case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There wouldn't be a difference, because each case would be different in this case. And not every value being checked is of type string (the array function call)

defaultValue = defaultModelValue || '';
} else if (type === 'boolean') {
defaultValue = defaultModelValue || false;
} else if (type === 'number') {
defaultValue = defaultModelValue || 0;
} else if (Array.isArray(defaultModelValue)) {
defaultValue = defaultModelValue || [];
} else {
defaultValue = defaultModelValue || null;
}

return defaultValue;
}
}

Expand Down
6 changes: 0 additions & 6 deletions generator/templates/Default/src/application/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ class User extends Model {
email: this.email,
};
}

mapResponse(data) {
this.id = data.id;
this.name = data.name;
this.email = data.email;
}
}

export default User;