Javascript 如何使用emberjs中的幻影伪数据进行分页?

Javascript 如何使用emberjs中的幻影伪数据进行分页?,javascript,ember.js,ember-data,ember-cli-mirage,Javascript,Ember.js,Ember Data,Ember Cli Mirage,我用海市蜃楼来伪造数据 场景/default.js export default function(server) { server.createList('product', 48); server.loadFixtures(); } this.get('/products', function(db) { let products = db.products; return { data: products.map(attrs => ({

我用海市蜃楼来伪造数据

场景/default.js

export default function(server) {
  server.createList('product', 48);
  server.loadFixtures();
}
this.get('/products', function(db) {
    let products = db.products;
    return {
      data: products.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });
上面我正在创建48个产品,我正在呼叫控制器

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

export default function(server) {
  server.createList('product', 48);
  server.loadFixtures();
}
this.get('/products', function(db) {
    let products = db.products;
    return {
      data: products.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });
现在我的问题是,如何加载每页10个产品?我正在发送过滤器10,因为页面大小和偏移量意味着页码


应该对config.js进行哪些更改以仅加载有限的产品?

在mirage/config.js中的处理程序中:

this.get('/products', function(db) {
    let images = db.images;
    return {
      data: images.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });
您可以访问请求对象,如下所示:

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
      }))
    };
  });
请看一个完整的示例。 其中,此旋转具有以下内容:

  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
    }
  })
您只需对其进行如下调整:

  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
      }))
    }
  })

在mirage/config.js中的处理程序中:

this.get('/products', function(db) {
    let images = db.images;
    return {
      data: images.map(attrs => ({
        type: 'product',
        id: attrs.id,
        attributes: attrs
      }))
    };
  });
您可以访问请求对象,如下所示:

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
      }))
    };
  });
请看一个完整的示例。 其中,此旋转具有以下内容:

  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
    }
  })
您只需对其进行如下调整:

  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
      }))
    }
  })

todos
为例,您可以根据自己的用例对其进行调整

    // 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
    });

todos
为例,您可以根据自己的用例对其进行调整

    // 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
    });

它运行良好。谢谢。我将db.images更改为db.products。您也可以更改它。它工作正常。谢谢。我将db.images更改为db.products。您也可以更改它。