最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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
    });
发布评论

评论列表(0)

  1. 暂无评论