Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. export class Index implements onInit{
  2. constructor (private http:Http){
  3. this.products : Observable<array>;
  4. this.page : Observable<array>;
  5. this.currentPage : Observable<number>;
  6. this.play = true;
  7. }
  8. ngOnInit() {
  9. this.currentPage = 0;
  10. this.getProducts();
  11. }
  12. getProducts(){
  13. var data = {"designer":"Leonardo"}
  14. var headers = new Headers();
  15. headers.append('Content-Type', 'application/json');
  16. this.http.post('/getProducts/', JSON.stringify(data), {
  17. headers: headers
  18. })
  19. .map(res => res.json())
  20. .subscribe(
  21. data => {
  22. this.devidePages(data);
  23. },
  24. err => console.log(err),
  25. () => console.log('Post Submited')
  26. );
  27. }
  28. devidePages(allproducts) {
  29. len = allproducts.length;
  30. var nrpages
  31. if ( Math.round(len/3) > parseInt(len / 3) ) {
  32. nrpages = Math.round(len/3) - 1;
  33. }
  34. else {
  35. nrpages = Math.round(len/3);
  36. }
  37. var j = -1;
  38.  
  39. var page = [];
  40. for (i in allproducts) {
  41. var element = allproducts[i];
  42. if (i%3 == 0) {
  43. j++;
  44. page[j] = [];
  45. page[j].push(element);
  46. }
  47. else {
  48. page[j].push(element);
  49. }
  50. }
  51. this.products.subscribe(page[this.currentPage]);
  52. this.page = page;
  53. this.startInterval(page,nrpages);
  54. }
  55. startInterval(page,nrpages) {
  56. console.log(this.products)
  57. setInterval(function() {
  58. if (this.currentPage < nrpages) {
  59. this.products.subscribe(page[this.currentPage]);
  60. this.currentPage++;
  61. } else {
  62. this.currentPage = 0;
  63. this.products.subscribe(page[this.currentPage]);
  64. // timer = $timeout(function() {
  65. // this.products = [];
  66. // }, 2500);
  67. },6000)
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement