Git Product home page Git Product logo

ember-infinite-scroll's Introduction

ember-infinite-scroll

Infinite scroll for your Ember app.

Installation

# From within your ember-cli project
ember install:addon ember-infinite-scroll

Usage

In your template:

<ul>
  {{#each}}
    <li>{{name}}</li>
  {{/each}}
</ul>
{{#infinite-scroll content=model hasMore=hasMore}}
  <span>Loading...</span>
{{/infinite-scroll}}

Simply display your list of items as you normally would and then add the infinite-scroll component directly after. Whatever is provided in the component block will only show up when more content is being fetched.

In the actions hash of your route/controller/component:

fetchMore: function(callback) {
  var promise = this.fetchMoreItems();
  callback(promise);
}

In order for everything to work correctly, it is critical that the callback function is passed the newly created promise that will resolve with the additional items.

If you need the scrollable element to be something other than window, just pass the element's selector as the scrollable option:

<ul>
  {{#each}}
    <li>{{name}}</li>
  {{/each}}
</ul>
{{#infinite-scroll content=model hasMore=hasMore scrollable='#scrollable'}}
  <span>Loading...</span>
{{/infinite-scroll}}

If you don't need to asynchronously request more data, you may want to follow this suggestion by @SirZach.

ember-infinite-scroll's People

Contributors

jasonkriss avatar jasonmit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ember-infinite-scroll's Issues

Allow scrollable element to be configured.

Jason,

Do you have any plans, or can you suggest a way, to configured the scrollable element? It's currently set to the window. However, I need to have a scrollable element that is a div within the page. It's actually quite close to working as is, but it does not work in IE 11. When I scroll my scrollable element in Chrome, the whole page "attempts" to scroll slightly, and I think that's why it is working in Chrome. However, IE 11 doesn't have that same behavior; the window/page doesn't move at all. I think being able to specify the scrollable element would be a nice addition, and would probably solve this problem.

Thanks,

TR

Can't get this working

I have added the component to the page:

        {{#infinite-scroll content=tasks hasMore=fetchMore}}
            <span>Loading...</span>
        {{/infinite-scroll}}

But when I scroll nothing happens. I also added a button:

<button type="button" class="btn" {{action fetchMore}}>More</button>

And this updaes the result set correctly. Any ideas why the plugin is not working for me?

possible mixin go-with

First off, just wanted to say thanks a lot for this gem! I ended up creating a mixin to go alongside it since I didn't actually need to asynchronously request more data. I'm posting it here in case you have any suggestions or want to incorporate it somehow.

import Ember from 'ember';

export default Ember.Mixin.create({
  /** The number of items to add to the page when you scroll down to the bottom **/
  chunkSize: 5,

  /** The array you should iterate over **/
  iterable: [],

  /**
   * You should implement this method to look something like this...
   * repopulateIterable: function () {
      this._super();

      this.get('iterable').pushObjects(this.get('metrics').slice(0, this.get('chunkSize'))); //hydrating iterable when the model you care about changes
    }.observes('metrics.[]'),
   */
  repopulateIterable: function () {
    this.set('iterable', []);
  },

  /**
   * Returns the array of data to be added to iterable, wrapped in a Promise
   * @param model
   * @returns {RSVP.Promise}
   */
  populateIterable: function (model) {
    var chunkSize = this.get('chunkSize'),
      iterableLength = this.get('iterable.length'),
      newData = model.slice(iterableLength, iterableLength + chunkSize);

    var promise = new Ember.RSVP.Promise(function (resolve, reject) {
      Ember.run(null, resolve, newData);
    });

    return promise;
  },

  /**
   * Return true if there are still more items to add to iterable
   * When false ember-infinite-scroll will no longer try to add more items to iterable
   */
  hasMore: function () {
    return this.get('iterable.length') < this.get('model.length');
  }.property('iterable.[]', 'model.[]'),

  actions: {
    fetchMore: function (callback) {
      var model = this.get('model');
      var promise = this.populateIterable(model);
      callback(promise);
    }
  }
});

Tests

Hey, do u plan to include some tests ? Thanks.

Assertion Failed: calling set on destroyed object

When I first enter the route with the ember-infinite-scoll component. everything works fine. When I leave the route and return, I start getting the error below and can no longer use the scroll functionality.

Uncaught Error: Assertion Failed: calling set on destroyed object
vendor.js:16896 Ember.assertvendor.js:30008 setvendor.js:44306 __exports__.default.Mixin.create.setember-infinite-scroll/components/infinite-scroll.js:30 __exports__.default.Em.Component.extend.didScrollvendor.js:13372 Backburner.runvendor.js:31556 applyvendor.js:30173 runvendor.js:31556 applyvendor.js:30215 run.joinvendor.js:31556 applyvendor.js:30267 run.bindvendor.js:4759 jQuery.event.dispatchvendor.js:4427 jQuery.event.add.elemData.handle

Any ideas why?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.