Our pure JavaScript Scheduler component


Post by dheitinga »

The endpoints we rely on for our stores are always POST request regardless of what the endpoint is used for. So in our case our Resource read endpoint (/Objects/Tree) is, instead of a GET request, a POST request which expects some data to be posted otherwise the endpoint will result in an error.

For reading resources I could simply 'patch' this by giving the store an empty object as body within the fetch options:

const resourceStore = new bryntum.scheduler.ResourceStore({
    readUrl: 'https://preview.wem.io/41834/webservices/Objects/Tree',
    autoLoad : true,
    useRestfulMethods : true,
    httpMethods : {
        create : 'POST',
        read : 'POST',
        update : 'PATCH',
        delete : 'DELETE'
    },
    headers : {
        'Content-Type': 'application/json'
    },

// Body property with empty object stringified ensures the endpoint above doesn't result in error
fetchOptions: {
    body: JSON.stringify({}),
    credentials: 'omit',
    redirect: 'error'
}
});

Unfortunatly using this approach will break the create, update and delete calls of the stores because using the fetchOptions.body property will overwrite the data posted by default.

Is there a way that we would only post an empty object in the case of the read calls of the stores and in all other cases post the data which is being sent by default?


Post by alex.l »

Hi dheitinga,

Here is an event that has access to body before request sent
https://bryntum.com/docs/scheduler/api/Core/data/AjaxStore#event-beforeRequest

Fired before any remote request is initiated.

and some others that may be useful for you https://bryntum.com/docs/scheduler/api/Core/data/AjaxStore#events

All the best,
Alex


Post by dheitinga »

Hey Alex,

I have tried implementing the beforeRequest event but it seems that the body is not updated before sending. I had something along these lines:

const isEmptyObject = (obj) => {
	// Function to check for empty objects, results in true when called below after a read request
}

const beforeRequest = (event) => {
	if (isEmptyObject(event.body)) {
		Object.assign(event, { body: {} })
	}
}

const eventStore = new bryntum.scheduler.EventStore({
	...
	
listeners : {
    	beforeRequest
}
});

Post by alex.l »

Looks like there is a bug in the code regarding this. I've opened a ticket here https://github.com/bryntum/support/issues/4999
It's marked as high-prio and will be fixed asap. Please subscribe on ticket updates by link provided to be notified when it's resolved.

All the best,
Alex


Post by Animal »

The issue is that the object which is sent as event.body is what actually gets sent. So if you change the property, it will make no difference.

You have to mutate the body object, not replace it. We can fix this as part of the ticket to extract the event.body and send that after the event handler has been called, then your original code would work.

For now, the below code will work:

eventStore : {
            readUrl: '/url',
            updateUrl: '/url',
            autoLoad : true,
            useRestfulMethods : true,
            httpMethods : {
                create : 'POST',
                read : 'POST',
                update : 'PATCH',
                delete : 'DELETE'
            },
            headers : {
                'Content-Type': 'application/json'
            },
            listeners : {
                beforeRequest : (event) => {
                    // Add properties of our own.
                    // The calling code already has a reference to `event.body` which
                    // it is going to send.
                    // So changing what that property references here makes no difference,
                    // but we can mutate the object
                    event.body.hello = 1;
                }
            }
        },

Post by dheitinga »

The solution above does keep the webservices from generating an error, but after implementing the scheduler doesn't stop loading. No further error messages are given in the console and or network tabs within the developer console. Looking in the network tab the request stopped showing up completely.

const resourceStore = new bryntum.scheduler.ResourceStore({
    readUrl: 'https://preview.wem.io/41834/webservices/Objects/Tree',
    autoLoad : true,
    useRestfulMethods : true,
    httpMethods : {
        create : 'POST',
        read : 'POST',
        update : 'PATCH',
        delete : 'DELETE'
    },
    headers : {
        'Content-Type': 'application/json'
    },

fetchOptions: {
    credentials: 'omit',
    redirect: 'error'
},

listeners: {
    beforeRequest: (event) => {
        // Add properties of our own.
        // The calling code already has a reference to `event.body` which
        // it is going to send.
        // So changing what that property references here makes no difference,
        // but we can mutate the object
        
        event.body.test = 1;
    }
}
});

Post by alex.l »

If it's while loading, please put debugger into beforeRequest and see if event.body is exists. I think that's the problem.
But recommended override will work for update operations only, we do not read/send body while loading. That's the actual problem the ticket about.
Meanwhile, ticked is fixed and merged. Since you don't have a license, you cannot download nightly build to get it. We will mail you soon with further instructions.

All the best,
Alex


Post by dheitinga »

The issue that resulted from this thread has been resolved and we are able to continue implementing the scheduler. Thanks for the support so far!


Post Reply