0

we need to arrange item in fixed format (by category) that are comming from multiple API's response ex: first to need to display all laptops, second display all PC's and so on..

    //fixed order
    this.itemList = [{
        id: '1', 
        name: 'Apple', 
        category: 'LAPTOP'
      }, {
        id: '2', 
        name: 'HP', 
        category: 'LAPTOP'
      }, {
        id: '3', 
        name: 'DELL', 
        category: 'PC'
      }, {
        id: '4', 
        name: 'SAMSUNG', 
        category: 'MOBILE'
      }, {
        id: '5', 
        name: 'LENOVO', 
        category: 'MOBILE'
      }];

here we don't want to call parallel api because of response delay, we need to show items according to api response but on ui the format fixed once all api response completed. Here is method for calling api:

loadItems() {
    this.itemList = [];
    this.itemService.getItems('LAPTOP').then((items) => {
      this.itemList = this.itemList.concat(items);
    });
    this.itemService.getItems('PC').then((items) => {
      this.itemList = this.itemList.concat(items);
    });
    this.itemService.getItems('TABLET').then((items) => {
      this.itemList = this.itemList.concat(items);
    });
    this.itemService.getItems('MOBILE').then((items) => {
      this.itemList = this.itemList.concat(items);
    });
}

So, if mobile api's response comes first, ui will display mobile items, after that if laptop api's response comes, it will show laptop first with maintain above format. thanks

2
  • can you explain what do you want exactly i can't understand Commented Apr 10, 2017 at 9:31
  • you can use orderBy in your repeat but that will have a particular order and not like whichever comes first Commented Apr 10, 2017 at 10:07

3 Answers 3

2

If you use Promise, Promise.all return responses in order:

loadItems() {
    this.itemList = [];
    Promise.all([
         this.itemService.getItems('LAPTOP'),
         this.itemService.getItems('PC'),
         this.itemService.getItems('TABLET'),
         this.itemService.getItems('MOBILE')
    ]).then((values) => {
         this.itemList = [].concat(values[0], values[1], values[2], values[3]);
    }).catch((err) => console.log('don\'t forget to handle error'));
}
Sign up to request clarification or add additional context in comments.

2 Comments

or, using $q of Angular, $q.all gets the same result
actully i don't want to call api in such a way(parallel) as i wrote above, because i need to display lazy load.
1

Ok, in this case, you could do something like this:

loadItems() {
    this.itemList = [ [], [], [], [] ];
    this.itemService.getItems('LAPTOP').then((items) => {
      this.itemList[0] = items;
      render(this.itemList);
    });
    this.itemService.getItems('PC').then((items) => {
      this.itemList[1] = items;
      render(this.itemList);
    });
    this.itemService.getItems('TABLET').then((items) => {
      this.itemList[2] = items;
      render(this.itemList);
    });
    this.itemService.getItems('MOBILE').then((items) => {
      this.itemList[3] = items;
      render(this.itemList);
    });
}

Where render is the function that will going to update your page with your new data :

function render(items) {
    var flatItems = [].concat.apply([], items);
    // do page update...
    ...
}

1 Comment

@Fexfux this is good option too but we implemented throgh angular filter, thanks for your effort, this is result actually want.
0

you can use promise .

suppose you have 4 methods

getLaptop();
getPc() ;
getTablet();
getMobile();

and use in chained methods like

 var dataList = [];
 $q.all({'laptop': getLaptop,'pc': getPc, 'tablet':getTablet, 'mobile':getMobile}).then(function(data) {
  angular.extend(dataList, data.laptop, data.pc, data.tablet, data.mobile);
}).catch(function(err) {
console.error('Error:', err);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.