javascript - how to do pagination using mirage fake data in emberjs? - Stack Overflow

I am using mirage for creating fake data.scenariodefault.jsexport default function(server) {server.cre

I am using mirage for creating fake data.

scenario/default.js

export default function(server) {
  server.createList('product', 48);
  server.loadFixtures();
}

Above I am creating 48 products and from controller I am calling

this.store.query('product', {
                filter: {
                    limit: 10,
                    offset: 0
                }
            }).then((result) => {
                console.log(result);
            });

and in mirage/config.js

this.get('/products', function(db) {
    let products = db.products;
    return {
      data: products.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });

now my question is, how to load 10 products per page? I am sending in filter 10 as page size and offset means page number.

what changes should be done to config.js to load only limited products?

I am using mirage for creating fake data.

scenario/default.js

export default function(server) {
  server.createList('product', 48);
  server.loadFixtures();
}

Above I am creating 48 products and from controller I am calling

this.store.query('product', {
                filter: {
                    limit: 10,
                    offset: 0
                }
            }).then((result) => {
                console.log(result);
            });

and in mirage/config.js

this.get('/products', function(db) {
    let products = db.products;
    return {
      data: products.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });

now my question is, how to load 10 products per page? I am sending in filter 10 as page size and offset means page number.

what changes should be done to config.js to load only limited products?

Share Improve this question edited May 10, 2016 at 6:31 murli2308 asked May 10, 2016 at 6:19 murli2308murli2308 3,0124 gold badges30 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

In your handler in mirage/config.js:

this.get('/products', function(db) {
    let images = db.images;
    return {
      data: images.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });

You are able to access the request object like so:

this.get('/products', function(db, request) {
    let images = db.images;
    //use request to limit images here
    return {
      data: images.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });

Have a look at this twiddle for a full example. Where the this twiddle has the following:

  this.get('tasks',function(schema, request){
    let qp = request.queryParams
    let page = parseInt(qp.page)
    let limit = parseInt(qp.limit)
    let start = page * limit
    let end = start + limit
    let filtered = tasks.slice(start,end)
    return {
      data: filtered
    }
  })

You'll just adapt it for your use like this:

  this.get('products',function(db, request){
    let qp = request.queryParams
    let offset = parseInt(qp.offset)
    let limit = parseInt(qp.limit)
    let start = offset * limit
    let end = start + limit
    let images = db.images.slice(start,end)
    return {
      data: images.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    }
  })

An example with todos, you can adapt it to your own use case.

    // Fetch all todos
    this.get("/todos", (schema, request) => {
        const {queryParams: { pageOffset, pageSize }} = request
        
        const todos = schema.db.todos;
    
        if (Number(pageSize)) {
            const start = Number(pageSize) * Number(pageOffset)
            const end = start + Number(pageSize)
            const page = todos.slice(start, end)
        
            return {
                items: page,
                nextPage: todos.length > end ? Number(pageOffset) + 1 : undefined,
            }
        }
        return todos
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745585862a4634535.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信