Question

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!

Was it helpful?

Solution

In your initialization use maxDataServiceVersion instead of dataServiceVersion.

I'm not sure why you'd need the above hack especially when using it with an unknown verb like UPDATE http://msdn.microsoft.com/en-us/library/dd541276.aspx. In OData there are two allowed HTTP Verbs MERGE for V2 and PATCH for V3. Once you set maxDataServiceVersion: 3.0 JayData will send out correct PATCH requests.

The hack your are using should be applied in a different situation, where either clients, proxies, routers don't support these two verbs. In that case you'd send a POST request instead and set the desired verb as X-HTTP-Method. This method is often referred as verb-tunneling see e.g. http://msdn.microsoft.com/en-us/library/dd541471.aspx.

$data.initService('/odata', { maxDataServiceVersion: '3.0' }).then(function (context) {
    if (!mycontext)
        mycontext= context;

   //this hack shouldn't be used here
   // 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();
});

Update based on comment

I'd check the context via the console. Make sure ctx is globally available e.g

window.ctx = context;

Attach something by ID e.g.

var role = ctx.Role.attachOrGet({ID: 'GUID or Int'});

role. will give your access to the entity properties e.g. assuming a Status property that accepts Int

role.Status = 1; //Set role.Status to 1

Last run ctx.saveChanges(). If everything is setup correctly JayData will send a PATCH request to the /odata/Role/GUID endpoint with the changes as JSON payload.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top