scala - play framework - what is the proper way to return list of data? -
i'm new reactive/observables concept.
i've defined route getting products
this:
get /products controllers.productcontroller.findproducts
and implementation in controller:
def findproducts() = secured.async { implicit request => productdao.find() .map(products => ok(json.tojson(products))) // products: list[product] .recover(errhandler) }
then, on client-side, i'm making call , subscribing this:
let sub = this.find() .subscribe( products => { this.products = products; this.productsbehaviorsubject.next( this.products ); if (!!sub) sub.unsubscribe(); }, error => console.log("error fetching units: " + error) );
as data, unsubscribe it. (basically using promises).
wonder, if right way it. instead of returning list[product]
in 1 single response, should returning in several responses??
def findproducts() = secured.async { implicit request => productdao.find() .map(products => { products.map(p => ok(json.tojson(p))) // haven't tried yet }) // products: list[product] .recover(errhandler) }
then on client side .. maybe .:
this.find() .take( 50 ) .subscribe( product => { this.products.push( product ) }, error => console.log("error fetching units: " + error), () => this.productsbehaviorsubject.next( this.products ) );
your first approach correct, notice secured.async
expects code block return future[result]
. if try map on products signature future[seq[result]]
give compilation error.
also in experience, returning full list in single response standard way code.
Comments
Post a Comment