获取数据的角度方式

I have site which is completely driven by ajax. When an user goes to some category page I want to show products there. What's the best way to fetch products for that category? I have 2 options:

  1. save all products into js variable threw php at first load of website, then when the user goes to some category filter this array to get only those products which has this category and send them to view

  2. dont save all produts into variable in the beggining, but fetch them threw api when an user touch some category

I think first option will be better because all products will be loaded in the begginging and then if an user touch 50 categories, I will not have to send 50 requests for products. But I'm not sure(maybe potencial problem if I have big amount of products)

Thanks.

You could also load all of the product data after the angular app has loaded.

At any rate, it vastly depends on how much product data you have and your current load time. It it's a small amount of products that combined only make up a 100kb json object, it doesn't really matter.

But if you have more than a trivial amount of data, and/or your average user is only accessing a small percentage of your product data, it makes more sense to load it on demand. You can always keep the data in memory so if the user switches back and forth from cat A -> cat B -> cat A, you only load each cat A & B once.

In the end, you just need to analyze your load times and how much data your thinking of loading. If loading it all in one go is still acceptably fast you should be fine, it it feels too slow then you need to optimize and load more things on demand as they are needed.