Quantcast
Viewing all articles
Browse latest Browse all 4826

Given an id how can I get the next/previous of a recordset relative to that id?

@kitsunde wrote:

I wrote some really awkward code earlier which basically filters through each item in the recordset on some condition and then selects the edge one.

export default Ember.Route.extend({
  model(params){
    const stories = this.store.findAll('story');

    const story = this.store.findRecord('story', params.id);

    const nextStory = story.then(function(story){
      const laterStories = stories.filter(function(filterStory){
        return filterStory.get('number') > story.get('number');
      });
      if(laterStories.get('firstObject')){
        return laterStories.get('firstObject');
      }else{
        return stories.get('firstObject');
      }
    });

    const previousStory = story.then(function(story){
      const newerStories = stories.filter(function(filterStory){
        return filterStory.get('number') < story.get('number');
      });
      if(newerStories.get('lastObject')){
        return newerStories.get('lastObject');
      }else{
        return stories.get('lastObject');
      }
    });
    return Ember.RSVP.hash({
      story,
      nextStory,
      previousStory
    });
  }
});

I know, it looks awful, there must be a nicer way of doing that. Any suggestions?

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4826

Trending Articles