Serenity: How to call a method in xxxRepository from Onclick method?

Created on 26 Apr 2018  路  2Comments  路  Source: serenity-is/Serenity

Hi , i'm new with serenity and i have one issue/doubt..
I have a grid who has all reserves of differents books from students, and when you click the checkbox in the row, it should enable my button 'APPROVE RETURN'.

Example:
imagen

My doubt is :

  • Im trying to call the method Update from xxxRepository from the OnClick method in the button, but i dont know if its possible.
    It requires a unit of work and a request that i don't know how obtain in my xxxGrid.ts .

METHOD in xxxRepository:
`public SaveResponse Update(IUnitOfWork uow, SaveRequest request)
{

        foreach (var libroId2 in request.Entity.LibrosList)
        {
            var rep = new LibrosRepository();
            var request2 = new RetrieveRequest() { EntityId = libroId2 };

            RetrieveResponse<Entities.LibrosRow> libroResult = rep.Retrieve(uow.Connection, request2);

            libroResult.Entity.Reservado = false;

            SaveRequest<Entities.LibrosRow> saveRequest = new SaveRequest<Entities.LibrosRow>();
            saveRequest.Entity = libroResult.Entity;

            rep.Update(uow, saveRequest);
        }

        request.Entity.FechaDevolucion = System.DateTime.Now;

        return new MySaveHandler().Process(uow, request, SaveRequestType.Update);
    }`

OnClick method:

`protected getButtons(): Serenity.ToolButton[]
{
var buttons = super.getButtons();

        buttons.push({
            title: 'Approve Test Button',
            cssClass: 'approve-button',
            onClick: () => {

             / /How i call the update method here?
            }
        })`

If it's not clear, please tell me so i try to explain better.
Thanks.

Most helpful comment

The repository is only accessible on the server side. Its access from the client side must be done by the Service class.
This class is generated by the xxxEndpoint.cs file (the generation of T4 will transpile it in your /Imports/ServerTypings/xxxService.ts).
If you look at its content, you will see that its methods are almost identical to the Repository, moreover, they call the Repository methods.

To use them on the client side, in your OnClick event, for example, you have call examples in the following file:
/Modules/Administration/RolePermission/RolePermissionDialogs.ts

Look this line :

                    click: e => {
                        RolePermissionService.Update({
                            RoleID: this.options.roleID,
                            Permissions: this.permissions.value.map(x => x.PermissionKey),
                            Module: null,
                            Submodule: null
                        }, response => {
                            this.dialogClose();
                            window.setTimeout(() => Q.notifySuccess(Q.text('Site.RolePermissionDialog.SaveSuccess')), 0);
                        });

Here we call the Update method of the RolePermissionService class on the client side. This is an interface to the RolePermissionEndpoint class on the server side.
The Update Method of RolePermissionEndpoint will call the Update method of RolePermissionRepository.cs

In this way, you do not have to worry about the Unit Of Work that will be implemented by the Serenity classes.

I invite you to also look at the documentation:
https://volkanceylan.gitbooks.io/serenity-guide/services/service_endpoints.html
https://volkanceylan.gitbooks.io/serenity-guide/services/list_request_handler.html

Best regards

All 2 comments

The repository is only accessible on the server side. Its access from the client side must be done by the Service class.
This class is generated by the xxxEndpoint.cs file (the generation of T4 will transpile it in your /Imports/ServerTypings/xxxService.ts).
If you look at its content, you will see that its methods are almost identical to the Repository, moreover, they call the Repository methods.

To use them on the client side, in your OnClick event, for example, you have call examples in the following file:
/Modules/Administration/RolePermission/RolePermissionDialogs.ts

Look this line :

                    click: e => {
                        RolePermissionService.Update({
                            RoleID: this.options.roleID,
                            Permissions: this.permissions.value.map(x => x.PermissionKey),
                            Module: null,
                            Submodule: null
                        }, response => {
                            this.dialogClose();
                            window.setTimeout(() => Q.notifySuccess(Q.text('Site.RolePermissionDialog.SaveSuccess')), 0);
                        });

Here we call the Update method of the RolePermissionService class on the client side. This is an interface to the RolePermissionEndpoint class on the server side.
The Update Method of RolePermissionEndpoint will call the Update method of RolePermissionRepository.cs

In this way, you do not have to worry about the Unit Of Work that will be implemented by the Serenity classes.

I invite you to also look at the documentation:
https://volkanceylan.gitbooks.io/serenity-guide/services/service_endpoints.html
https://volkanceylan.gitbooks.io/serenity-guide/services/list_request_handler.html

Best regards

Thanks for the help @Doomisateur , i'll try it .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stixoffire picture stixoffire  路  3Comments

stepankurdylo picture stepankurdylo  路  3Comments

AmuthaKondusamy picture AmuthaKondusamy  路  3Comments

GitHubOrim picture GitHubOrim  路  3Comments

chintankukadiya18 picture chintankukadiya18  路  3Comments