I have initialized a jaydata context:
$data.initService('/odata/$metadata', { dataServiceVersion: '3.0' }).then(function (context) {
if (!mycontext)
mycontext= context;
//this is a hack
mycontext.prepareRequest = function (r) {
if (r[0].method == "PATCH") {
r[0].method = 'PUT';
r[0].headers['X-HTTP-METHOD'] = 'UPDATE';
}
};
vm.roles = mycontext.Role.toLiveArray();
vm.users = mycontext.User.toLiveArray();
});
The workaround with mycontext.prepareRequest was done after finding this issue on github
The list of initialized roles and users gets displayed successfully.However I'm still having issues updating the an entity:
vm.updateRole = function (r) {
r.Name = 'NewUpdatedName';
zivacontext.Role.attach(r);
r.entityState = $data.EntityState.Modified;
zivacontext.saveChanges().then(function (result) {
debugger;
});
};
The request gets redirect to the UpdateEntity method of the the provided below controller, however only the Id property of the entity is set. The other properties are NULL, the same goes if the request gets redirected to PatchEntity method (when the workaround hack is not applied) the changed fields are not passed to the delta. In the Request Payload in both cases (with the hack or not) only the Id is passed to the server.
The controller:
public class BaseODataController : EntitySetController where TEntity : class where TEntityDto : class where TIdentityType : class where TService : ICrudService { // ...
protected override TEntityDto UpdateEntity(TIdentityType key, TEntityDto update) { _service.Update(update); return base.UpdateEntity(key, update); }
protected override TEntityDto PatchEntity(TIdentityType key, Delta<TEntityDto> patch)
{
return base.PatchEntity(key, patch);
}
// ... }
Also while debugging I can see that the entity has tracked the changes:
r: RoleDto
$$hashKey: "00I"
Description: (...)
Id: (...)
Name: (...)
ValidationErrors: (...)
_ValidationErrors: Array[0]
_changedProperties: Array[2]
0: MemberDefinition
1: MemberDefinition
configurable: true
dataType: function String() { [native code] }
definedBy: function RoleDto(){
enumerable: true
kind: "property"
name: "Name"
originalType: "Edm.String"
type: function String() { [native code] }
__proto__: MemberDefinition
length: 2
__proto__: Array[0]
_entityState: 30
_isDirty: true
_isNew: false
The only thing i could not understand was why the ValidationErrors were in the _changedProperties:
_changedProperties: Array[2]
0: MemberDefinition
configurable: true
dataType: function Array() { [native code] }
definedBy: function Entity(){
elementType: function ValidationError(){
enumerable: false
kind: "property"
monitorChanges: true
name: "ValidationErrors"
notMapped: true
originalElementType: function ValidationError(){
originalType: function Array() { [native code] }
storeOnObject: true
type: function Array() { [native code] }
__proto__: MemberDefinition
So the question here is why the changes are not getting passed in the Request Payload to the server?
Thanks in advance!