Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store idChange event not fired #6347

Closed
chuckn0rris opened this issue Mar 10, 2023 · 2 comments
Closed

Store idChange event not fired #6347

chuckn0rris opened this issue Mar 10, 2023 · 2 comments
Assignees
Labels
bug Something isn't working forum Issues from forum large-account Reported by large customer OEM OEM customer premium resolved Fixed but not yet released (available in the nightly builds)
Milestone

Comments

@chuckn0rris
Copy link

Forum post

Hello,
we recently figured out the rootcause for a problem we were having in our gantt solution. We implement the CrudManger to communicate with our remote backend (autoSync is enabled). So after each sync we get back the actual record ids to unphantom added records. We had the same issue as described in #4935 even tough we were using a version that included the fix for that. We made a workaround which overrode the the "id" field getter of the "Location" class. This only partly solved random issues we were having which now turned out to be related to the "selectedRecordsCollection" of the gantt.

After debugging for a good amount of time, we figured out, that the "idChange" event in the task store was never fired. The "onModelChange" method in the Store class get's called twice when adding a record. First with the initial data and an assinged phantom id and a second time when the sync respone has been applied and an "id" has been provided. The problem seems to be logic in the method "onModelChange" of the class "ChronoPartOfProjectStoreMixin" that overrides the "silent" arugment which than gets passed to the "Store" instance "onModelChange" method. In the "Store.onModelChange" the "idChange" event is only fired, if silent is not "false". Since the argument is overriden to "true", the event is not fired when a record "id" has been proviced in the sync application. This results in a bunch of things not happening that are listening to that event, like rebuilding selection indicies and the bugfix mentioned above.

We have worked around this with an override of the "Store.onModelChange" as following which solves our problem:

const anyStore = Store as any;
		const originalOnModelChange = anyStore.prototype.onModelChange;

	anyStore.prototype.onModelChange = function onModelChange(
		this: Store,
		record: Model,
		toSet: ToSetDict,
		wasSet: ToSetDict,
		silent: boolean,
		fromRelationUpdate: boolean
	) {
		// Call original method
		originalOnModelChange.call(this, record, toSet, wasSet, silent, fromRelationUpdate);

		if (silent && 'id' in wasSet) {
			const { oldValue, value } = toSet.id;

			this.trigger('idChange', {
				store: this,
				record,
				oldValue,
				value,
			});
		}
	};

Since none of the online examples have a remote backend, we couldn't easily validate, if the behaviour also happens in an example. Since the PHP sample is the only sample with a remote backend and requires local installation of dependencies, we couldn't test it due to time constraints.

I hope this is enough inforation for you guys to investigate this. It's important to use a remote backend for properly check this. Running the PHP sample locally and debugging according to the above insturctions should show the issue.

@chuckn0rris chuckn0rris added bug Something isn't working premium forum Issues from forum large-account Reported by large customer OEM OEM customer labels Mar 10, 2023
@arcady-zherdev
Copy link

Considering the code this happens if a record has changed both its id and some other field served by the Engine.

Test case for the issue:

    t.it('Should trigger idChange when responding it from the server together w/ some other Engine handled field', async t => {

        t.mockUrl('load', {
            responseText : {
                tasks : {
                    rows : [
                        {
                            id        : 1,
                            name      : 'Task',
                            startDate : '2023-03-10',
                            duration  : 1
                        }
                    ]
                }
            }
        });

        const project = new ProjectModel({
            loadUrl : 'load',
            syncUrl : 'sync'
        });

        t.firesOnce(project.eventStore, 'idChange');

        await project.load();

        t.notOk(project.getChangesetPackage(), 'No data in set package exists before sync');

        const [newTask] = project.eventStore.add({ name : 'Task 2', duration : 1 });

        t.mockUrl('sync', {
            responseText : {
                tasks : {
                    rows : [
                        {
                            $PhantomId : newTask.id,
                            id         : 2,
                            duration   : 2
                        }
                    ]
                }
            }
        });

        await project.sync();

        t.is(newTask.get('id'), 2, 'id has been successfully updated');
        t.is(newTask.get('endDate'), new Date(2023, 2, 12), 'End date has been successfully updated');

        project.destroy();
    });

@arcady-zherdev
Copy link

initial progress pushed to 6347-idChange branch

@canonic-epicure canonic-epicure self-assigned this Jun 29, 2023
@canonic-epicure canonic-epicure added in progress ready for review Issue is fixed, the pull request is being reviewed and removed in progress labels Jul 3, 2023
@arcady-zherdev arcady-zherdev added this to the 5.4.1 milestone Jul 4, 2023
@arcady-zherdev arcady-zherdev added resolved Fixed but not yet released (available in the nightly builds) and removed ready for review Issue is fixed, the pull request is being reviewed labels Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working forum Issues from forum large-account Reported by large customer OEM OEM customer premium resolved Fixed but not yet released (available in the nightly builds)
Projects
None yet
Development

No branches or pull requests

3 participants