Skip to content
This repository has been archived by the owner on May 17, 2018. It is now read-only.

state is not updated before event handlers are fired #27

Closed
alanrubin opened this issue Jan 16, 2013 · 3 comments
Closed

state is not updated before event handlers are fired #27

alanrubin opened this issue Jan 16, 2013 · 3 comments
Labels
Milestone

Comments

@alanrubin
Copy link

When reseting the fullCollection with new models, the "state" (most specifically the totalRecords) are updated after the pageableCollection is reseted (backbone-pageable line 475-477).

In my case, the scenario is the following: If I have a pagination Backbone.View (renders the pagination controls for navigating - forward, back, pages number) listening to the "reset" event in the pageableCollection, it will be rendered with the previous state when a reset is done. That could be true for the event "remove" as well, but I still have to check that.

My tentative fix is the below code. I will add a test to demonstrate the issue.

if (event == "reset" || event == "sort") {
        options = collection;
        state.totalRecords = this.models.length;
        pageableCollection.state = pageableCollection._checkState(state);
        resetQuickly(pageableCollection, this.models.slice(pageStart, pageEnd),
                     options);
}
@wyuenho
Copy link
Member

wyuenho commented Jan 18, 2013

If I understand this issue correctly, you have an event listening that needs to access the state of the collection and you need all the states to be updated when u access them? This can be tricky since Backbone event handlers are applied backwards. So add, remove, reset, sort are all affected...

@wyuenho
Copy link
Member

wyuenho commented Jan 18, 2013

Actually I was wrong, event handlers are fired in order in Backbone.

I think this problem is caused by the PageableCollection event handler is listening on all and the state is updated inside the all event handler, but all is fired after all the other more granular event are fired first so if you are listening to those, you don't see the updated state.

@wyuenho
Copy link
Member

wyuenho commented Jan 18, 2013

Ok this will take me a while to fix. For now I think you should listen to all and detect the event type passed instead as a workaround. It will be forward compatible even after I've made the fix.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants