Update method with post - javascript

I have to create an Update method with a post request.
But when I did it , create an another object.
this is the python code from the server for edit class
def edit_schema(cls, admin: bool = False, cm: bool = False):
if cm:
return {
'id': {'type': 'number', 'min': 1},
'client': {'type': 'number', 'required': False, 'min': -1, 'forbidden': [0]},
'led_spec': {'type': 'string', 'required': False}
}
elif admin:
return {
'id': {'type': 'number', 'min': 1},
'client': {'type': 'number', 'required': False, 'dependencies': 'group', 'min': -1, 'forbidden': [0]},
'name': {'type': 'string', 'required': False, 'empty': False, 'maxlength': 255},
'led_spec': {'type': 'string', 'required': False}
# }
else:
return {
'id': {'type': 'number', 'min': 1},
'name': {'type': 'string', 'required': False, 'empty': False, 'maxlength': 255}
}
My service.ts
public update(fixture: Fixture){
let body = {};
if(fixture.name !== '' && fixture.name !== null)
body['name'] = fixture.name;
if(fixture.client !== -1 && fixture.client !== null)
body['client'] = fixture.client;
if(fixture.group !== -1 && fixture.group !== null)
body['group'] = fixture.group;
return this
.post('fixture/edit', body).toPromise().then(data => {
this._refreshNeeded$.next();
})
}
and my component
update() {
if (this.form.valid) {
this.formSubmitAttempt = true;
this.fixtureService.update(this.form.value);
this.dialogRef.close(this.form.value);
console.log(this.form.value)// send updated value to main component
}
}
```

Related

JS: Do I override or add in fields when use _.extend() in express?

When I have an object and I put it as an extension of another object in which I put value for some of the extended fields - is it going to be rewritten or the value will be added to the old one?
For example if I have:
const PATCH_REQUEST_SCHEMA = {
'type': 'object',
'title': 'Some object',
'description': 'Some object Representation',
'properties': {
'name': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Name'
}
};
const POST_REQUEST_SCHEMA = _.extend({}, PATCH_REQUEST_SCHEMA, {
'properties': {
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}
});
What would be the result for POST_REQUEST_SCHEMA.properties ?
Will it be:
{
'name': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Name'
},
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}
or:
{
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}
I tested it manually - it rewrites/overrides the field "properties". So the answer is:
{
'surname': {
'type': 'string',
'minLength': 1,
'maxLength': 256,
'title': 'Surname'
}
}

Angular ngModel, 3 arrays are affected

I am creating a form builder in Angular, I used Lam Phuoc ThinhI work for reference, check his work here
<https://codepen.io/thinhlam/pen/BowEPp>
Now the problem is I have this 3 arrays, Current Field, Form Fields and Draggable Fields. Everytime I dragged a field, like for example the Header Field and then I changed the text or value of that field, the Draggable Fields is also affected. So if i dragged another Header field the value is the same with the first header field that i drag. Please check the image below.
image
app.component.html
<div *ngFor="let set of CurrentField.Settings; let i = index; trackBy:trackByIdx" [ngSwitch]="set.Type">
<div *ngSwitchCase="'textarea'" class="form-group">
<label for="textarea-{{i}}">{{set.Name}}</label>
<input [name]="'textarea' + i" type="text" class="form-control" [(ngModel)]="set.Value">
</div>
</div>
app.component.ts
FormFields: any;
CurrentField: any;
guid: number;
DragElements = [{
'Name': "Header",
"Type": "textLabel",
'Icon': "fa fa-header",
'Settings': [{
'Name': 'Field Label',
'Value': 'Header',
'Type': 'textarea'
}]
}, {
'Name': "Columns",
"Type": "columns",
'Icon': "fa fa-columns",
'Settings': [{
'Name': 'Number of Columns',
'Value': 0,
'PossibleValue': [2,3,4]
}, {
'Name': 'Column',
'Columns': []
}],
}, {
'Name': "Image",
"Type": "image",
'Icon': "fa fa-image",
'Settings': [{
'Name': 'Field Label',
'Value': 'Image',
'Type': 'textarea'
}]
}, {
'Name': "Single Text",
'Type': "text",
'Icon': "fa fa-font",
'Settings': [{
'Name': 'Field Label',
'Value': 'Single Text',
'Type': 'text'
}]
}, {
'Name': "Number",
'Type': "number",
'Icon': "fa fa-th-large",
'Settings': [{
'Name': 'Field Label',
'Value': 'Number',
'Type': 'text'
}]
}, {
'Name': "Date",
'Type': "date",
'Icon': "fa fa-calendar",
'Settings': [{
'Name': 'Field Label',
'Value': 'Date',
'Type': 'text'
}]
}, {
'Name': "Single Selection",
"Type": "dropdown",
'Icon': "fa fa-dot-circle-o",
'Settings': [{
'Name': 'Field Label',
'Value': 'Single Selection',
'Type': 'text'
}]
}, {
'Name': "Pagaraph Text",
"Type": "textarea",
'Icon': "fa fa-align-left",
'Settings': [{
'Name': 'Field Label',
'Value': 'Paragraph Text',
'Type': 'text'
}]
}];
constructor(
private cd: ChangeDetectorRef,
private zone: NgZone,
) {}
ngOnInit() {
this.CurrentField = {};
this.FormFields = [];
this.guid = 1;
}
createNewField = function() {
return {
'id': ++this.guid,
'Name': '',
'Settings': [],
'Active': true,
'ChangeFieldSetting': function(Value, SettingName) {
switch (SettingName) {
// case 'Field Label':
// this.Settings[0].Value = Value;
// // console.log(Value);
// break;
case 'Image Alignment':
this.Settings[2].DefaultAlignment = Value;
break;
case 'Columns':
let columns : any = {};
this.Settings[0].Value = Value;
for(let i = 0; i < Value; i++) {
columns = {
'ID': i + "-" + this.id,
'Title': 'Column '+i,
'Items': []
}
this.Settings[1].Columns.push(columns);
}
console.log(this);
break;
default:
break;
}
},
'GetFieldSetting': function(settingName) {
let result = {};
let settings = this.Settings;
settings.forEach((index, set) => {
if (index.Name == settingName) {
result = index;
return;
}
});
if (!Object.keys(result).length) {
settings[settings.length - 1].Options.forEach((index, set) => {
if (index.Name == settingName) {
result = index;
return;
}
});
}
return result;
}
};
}
changeFieldName = function(Value) {
this.CurrentField.Name = Value;
this.CurrentField.Settings[0].Value = this.CurrentField.Name;
}
removeElement = function(idx){
if(this.FormFields[idx].Active) {
this.CurrentField = {};
}
this.FormFields.splice(idx, 1);
};
addElement(ele, idx) {
// this.zone.run(() => {
this.CurrentField.Active = false;
this.CurrentField = this.createNewField();
Object.assign(this.CurrentField, ele);
if (idx == null) {
this.FormFields.push(this.CurrentField);
} else {
this.FormFields.splice(idx, 0, this.CurrentField);
}
// });
};
activeField = function(f) {
this.CurrentField.Active = false;
this.CurrentField = f;
f.Active = true;
};
Wherever you are using
Object.assign(this.CurrentField,ele);
like operations
deep clone before pushing.
Object.assign(this.CurrentField, JSON.parse(JSON.stringify(ele)));
This is one way of deep cloning which has its pros and cons. But your solution will be to deep clone using any suitable method.

JayData : how to migrate code from v1.3 to v1.5

I have some code which worked in JayData 1.3.
I need to upgrade JayData to 1.5 due to the compatibility issues the 1.3 version has with polymer.
The upgrade instructions say you can use the "jaydata-compatibility.js" script to "upgrade your app to JayData 1.5.x from previous versions step-by-step", however when I add that in as described I simply get the error, "typeOrName requires a value other than undefined or null", which actually doesn't help me step through the upgrade at all.
Here is the JayData 1.3 code :
$data.Entity.extend('Cache', {
'id': { 'type': 'int', 'key': true, 'computed': true },
'url': { 'type': 'string' },
'method': { 'type': 'string', 'required': true },
'dts': { 'type': 'string', 'required': true },
'encryptMeth': { 'type': 'string' },
'data': { 'type': 'string' }
});
$data.EntityContext.extend('APIWrapperDB', {
'Cache': { 'type': $data.EntitySet, 'elementType': Cache }
});
var cacheDatabase = new APIWrapperDB('TheAPIWrapperDatabase');
cacheDatabase.onReady( function() { /* now my DB is ready */ };
What is the JayData 1.5 equalivalent of this code?
This is the updated snippet, I've just declared your entity definitions as variables as JayData stopped using global objects.
var Cache = data.Entity.extend('Cache', {
'id': { 'type': 'int', 'key': true, 'computed': true },
'url': { 'type': 'string' },
'method': { 'type': 'string', 'required': true },
'dts': { 'type': 'string', 'required': true },
'encryptMeth': { 'type': 'string' },
'data': { 'type': 'string' }
});
var APIWrapperDB = $data.EntityContext.extend('APIWrapperDB', {
'Cache': { 'type': $data.EntitySet, 'elementType': Cache }
});
var cacheDatabase = new APIWrapperDB('TheAPIWrapperDatabase');
cacheDatabase.onReady( function() { /* now my DB is ready */ };

Angular.Copy does not allow me to create unique objects in a separate array

I'm building a configurator in AngularJS that uses the object $scope.data to allow users to edit the object via the front-end. The user can then save this object to a separate array in $scope.builds, allowing them to have multiple configurations. Here is the function that adds another configuration to $scope.builds.
$scope.addition = function(){
var newData = angular.copy($scope.data);
$scope.builds.push(newData);
}
Unfortunately, despite using the angular.copy function, all of the objects in the array of $scope.builds seem to be the same $scope.data object duplicated over and over.
EDIT:
Here is an abridged version of what $scope.data looks like:
$scope.data = [
{
'title': 'Select your configuration',
'required': true,
'options': [
{
'name': 'Option 1',
'select': true,
'filter': true
}, {
'name': 'Option 2',
'select': false,
'filter': true
}, {
'name': 'Option 3',
'select': false,
'filter': true
}
]
}, {
'title': 'Select your configuration',
'required': true,
'options': [
{
'name': 'Option 1',
'select': true,
'filter': true
}, {
'name': 'Option 2',
'select': false,
'filter': true
}, {
'name': 'Option 3',
'select': false,
'filter': true
}
]
}
];

I want to access inputed Value in JSON array using javascript

i created array 'PROPERTIES' which accept value like username, password,sid etc.And i want to access this entered value using javascript.
'PROPERTIES': {'gatewayurl':
{'Name':
{
'en': 'Input gatewayurl'
},
'Description': {
'en': 'Input gatewayurl for sms gateway'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '',
},
'username':
{'Name':
{
'en': 'Input username'
},
'Description': {
'en': 'Input username'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '{=Document:NAME}',
},
'password':
{'Name':
{
'en': 'Input string'
},
'Description': {
'en': 'Input password'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '{=Document:NAME}',
},
'sid':
{'Name':
{
'en': 'Input string'
},
'Description': {
'en': 'Input sid'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': '{=Document:NAME}',
},
'mobileno':
{'Name':
{
'en': 'Input mobileno'
},
'Description': {
'en': 'Input mobileno'
},
'Type': 'number',
'Required': 'Y',
'Multiple': 'N',
'Default': null,
},
'message':
{'Name':
{
'en': 'Input message'
},
'Description': {
'en': 'Input message'
},
'Type': 'string',
'Required': 'Y',
'Multiple': 'N',
'Default': null,
}
},
i Used php code But i want Access value using only Javascript Please Some one Help me
$url =$_REQUEST["properties"]["gatewayurl"];
$user = $_REQUEST["properties"]["username"];
$password = $_REQUEST["properties"]["password"];
$sid = $_REQUEST["properties"]["sid"];
$number= $_REQUEST["properties"]["mobileno"];
$message= $_REQUEST["properties"]["message"];
If "PROPERTIES" is an array as you were saying you should initialize it as an array.
var PROPERTIES = [ {'gatewayurl' ........... }]
Then you can get values by
var url = PROPERTIES[0].gatewayurl;
var user = PROPERTIES[0].username;
var password = PROPERTIES[0].password;
var sid = PROPERTIES[0].sid;
etc..
If its a JSON Array as shown in your example,
var REQUEST = {
'PROPERTIES': {'gatewayurl' ... }
}
Then you can get values by
var url = REQUEST["properties"]["gatewayurl"];
var user = REQUEST["properties"]["username"];
var password = REQUEST["properties"]["password"];
var sid = REQUEST["properties"]["sid"];

Categories

Resources