t4jaydata

JayData Data model already created - add an OData action afterwards


I have an OData Web Api service where I create the controllers with a T4 template from the EF datamodel. While doing that, I also create the Jaydata datamodel with T4.

But now, I have a partial class that will add an action to one of my controllers.

As the JayData file is also created by a T4 template, is there a way to add actions to one of the EntitySets later on?


Solution

  • What I managed to do now is the following: My generated JayData context looks like the following:

    $data.EntityContext.extend('myNameSpace.MyContext', {
        'Cases': { type: $data.EntitySet, elementType: myNameSpace.Case},
        // ... other Entitysets
    }
    

    Later, I extend this context like this:

    myNameSpace.MyContext.extend('myNameSpace.MyExtendedContext', {
        'Cases': { type: $data.EntitySet, elementType: myNameSpace.Case, actions: {
            'Checkout': { type: $data.ServiceAction, returnType: 'myNameSpace.Case', IsBindable: true, 'EntitySet': 'Cases', IsAlwaysBindable: true, params: [{ name: 'Id', type: 'Edm.Guid' }] }
        }
    }
    

    So I can use my action if I later use my extended context. I think this should be good enough.

    My Typescript definitions for this look like this:

    declare module myNamespace {
    export class CaseExtensions extends $data.EntitySet<myNamespace.Case> {
        Checkout: {
            (Id: string, handler?: (result: myNamespace.Case) => void): $data.IPromise<Case>;
            (params?: { Id?: string; }, handler?: (result: myNamespace.Case) => void): $data.IPromise<Case>;
        };
    }
    
    export class MyExtendedContext extends MyContext {
        Cases: CaseExtensions;
    }
    }