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

Doesn't load data #190

Closed
danilovaz opened this issue Oct 27, 2016 · 5 comments
Closed

Doesn't load data #190

danilovaz opened this issue Oct 27, 2016 · 5 comments

Comments

@danilovaz
Copy link

I'm using Ember 2.9.0 and I'm loading a list of Chat, so I don't know how many total_pages it's have.

The first time that renders the template/index loads the Chat list with 2 chats. But when I scroll down to, nothing is triggered to fetch the next page.

My page [number] is not sequential (1,2,3,4) but a random number (3812, 23894, 31283, 47472), maybe that's the problem?

I am using JSONAPI and the link to the next is like this:

date: {...} // date of the model API
include: {...} // include the model API
links: {
   next: {
     number: 3812,
     limit: 2
   }
}

And my routes/chats/index is this:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
import InfinityRoute from "ember-infinity/mixins/route";

const { RSVP: { Promise }, $: { ajax }, run } = Ember;

export default Ember.Route.extend(AuthenticatedRouteMixin, InfinityRoute, {
  _canLoadMore: true,

  perPageParam: "page[limit]",              // instead of "per_page"
  pageParam: "page[number]",              // instead of "page"

  beforeModel() {
    moment.locale('pt-br');
  },

  model(params) {
    return Ember.RSVP.hash({
      profiles: this.store.findAll('profile'),
      channels: this.store.findAll('channel'),
      channelAccounts: this.store.findAll('channel-account'),
      channelApplications: this.store.query('channel-application', { filter: { feature: 'inbox'} }).then(function(data) {
        return data
      }),

      chats: this.infinityModel("chat", { perPage: 2, startingPage: 0, modelPath: 'controller.model.chats' })
    })
  },

  infinityModelUpdated() {
    Ember.Logger.debug('updated with more items');
  },
  infinityModelLoaded(lastPageLoaded, infinityModel) {
    Ember.Logger.info('no more items to load');
  }
});

I'm loading the template/index this way:

<ul id="loadchat" class="inbox-list-items">
   {{#each model.chats as | chat index |}}
     {{list-chat chat=cha index=index selectChat='selChat'}}
   {{/each}}

   {{infinity-loader scrollable="#loadchat" infinityModel=model.chats loadingText="Loading ..." loadedText="Loaded"}}
</ul>

So I try to follow the solution with modelPath and

setupController(controller, model) {
    controller.setProperties(model);
  }

But this doesn't work, when I use setupController my model doesn't load data. Any solution to fix this to work without total_page? Or anything I'm doing wrong?

@hhff
Copy link
Collaborator

hhff commented Oct 27, 2016

Hi @danilovaz !

Got it.

This is kinda a hack - but could you try adding this hook to your route?

  afterInfinityModel() {
    this.set('_totalPages', 999999);
  }

I'm working on a modularization of Ember Infinity that should fix this, but right now it's not quite ready.

@danilovaz
Copy link
Author

@hhff Thank's man! That seems to be working, but not completely. See, my API expects a page[number] not sequential. But Ember Infinity is setting page[number]=2 when I scroll to nextPage. I need to pass the value that came from API in number:

links: {
    next: {
      number: 3812,
      limit: 2
    }
}

I can do that?

@hhff
Copy link
Collaborator

hhff commented Oct 27, 2016

also a hack, but try this

  afterInfinityModel(newObjects) {
    let nextPage = newObjects.get('links.next.number');
    console.log(nextPage); //<-- make sure this works?

    this.set('nextPageToLoad', nextPage);
    this.set('_totalPages', 999999);
  },

  _buildParams() {
    let params = this._super(...arguments);
    params[this.get('pageParam')] = this.get('nextPageToLoad');
    return params;
  }

@danilovaz
Copy link
Author

You saved my life twice. Thank you :)

Now I just need to keep it from endlessly load the same items. I have 5 items on the list when it comes on the last item its reloads again from the first item, infinitely.

But it's working now. Thank you. As soon as I get free, I'll see what I can help with PR's.

@hhff
Copy link
Collaborator

hhff commented Oct 27, 2016

cool! thankyou!

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

No branches or pull requests

2 participants