2016-05-10 6 views
0

偽のデータを作成するためにmirageを使用しています。私は48個の製品を作成していますし、コントローラから、私はemberjsでミラージュの偽データを使用してページネーションを行う方法は?

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

蜃気楼/ config.jsので

this.get('/products', function(db) { 
    let products = db.products; 
    return { 
     data: products.map(attrs => ({ 
     type: 'product', 
     id: attrs.id, 
     attributes: attrs 
     })) 
    }; 
    }); 
を呼び出しています上記

シナリオ/にDefault.js

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

今私のクエストページあたり10個の製品をロードする方法は?私は、ページサイズとオフセットはページ番号を意味するフィルタ10を送信しています。

限定された製品のみをロードするためにconfig.jsにどのような変更を行う必要がありますか?蜃気楼/ config.jsの中にあなたのハンドラで

答えて

2

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 twiddleを見てください。

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

それがうまく機能しているこの責めは以下があります 。 db.productsにdb.imagesを変更しました。変更することもできます。 – murli2308

関連する問題