adopted-ember-addons / ember-infinity

:zap: Simple, flexible Infinite Scroll for Ember CLI Apps.
MIT License
377 stars 131 forks source link

Support Server Polling #335

Open maprules1000 opened 6 years ago

maprules1000 commented 6 years ago

Hi!

I ran into a situation where I want my page to be infinitely scrollable but also populate the top of the page with new objects that come in from the server.

So right now I have an infinity model that successfully loads data, but I have a polling event that refreshes some of the loaded data, which breaks data relationships in the infinity model.

I was able to fix it for my use case, but it would be ideal to incorporate an update option that doesn't break relationships of non-updated objects.

It would be great if I could ask the infinity model to basically make the first call again and have it update the store with new objects, or any objects that have been updated.

Here's the polling code (which is managed by ember-concurrency):

this.get('item').reload().then(item => {
    item.get('allOrders').reload()
});

And the infinity model is loaded through a model hook on a route.

I should also note that I'm using cursor based pagination, and the results coming back from the server are already ordered.

johncalvinyoung commented 6 years ago

Perhaps I'm misunderstanding, but how does it 'break data relationships' in the model?

Also, if you implement some logic to determine your delta, would unshiftObjects be of any use?

maprules1000 commented 5 years ago

To be honest, I'm not really sure how it breaks some data relationships, but there are several hasMany/belongsTo relationships that no longer exist after attempting to reload the allOrders relationship.

In the template I have:

{{#each allOrders as |order|}}
  <!-- html stuff -->
  {{order.item.name}}
{{/each}}

Where item is another model that hasMany orders.

This properly renders when the infinity model is called, but as soon as alllOrders.reload() is run the relationship disappears from the template. The data for the orders is still in the template, and the item data still exists in the store, but it looks like the reloaded data no longer has a working hasMany/belongsTo relationship and the item data disappears from the template without an error. It seems like using the vanilla Ember Data reload() call overwrites and breaks something with the infinityModel.

I'm still digging into the cause, but it would be great if the ember-infinity service had an "in-house" way to poll for new data and update the existing infinity models that have been already loaded, or even just re-run the initial server call. However, it might be tricky to merge the updated data into the existing infinityModel array.

I think unshiftObjects might be useful to solving the problem, but I'll have to investigate it more since it's not really documented in the README.