Search code examples
ember-cli

Ember CLI - Custom routing for very basic dropdown


New to ember and ember cli, and not having any JS based framework experience apart from jQuery (which is not a framework)

I find my self stuck at the very beginning compared to work done in Angular,

I have a static list of groups which are on REST api `http://localhost:8000/api/groups' and it is only 8 items there, I needed to show them as dropdown for a search criteria. Nothing fancy

I started with creating a route and model with the name of groups but app stopped working and I had to create a model for group which is identical to groups model, only having 2 items for dropdown

Now i have a url in my ember app http://localhost:4200/groups which I dont need and i dont want it to be there,

But I ignored it and had to create a dropdown of the cities, api is

http://localhost:8000/api/cities <-- All cities list, needed for admin
http://localhost:8000/api/cities/active <-- For clients to show active cities so they can add their records
http://localhost:8000/api/cities/filled <-- For users, to show them where clients are available

So in ember, I created a route and model for cities, and same model is copied as city just to show the list of cities in dropdown, I needed to show cities which are filled, I had created ember g route city/filled and it created folders, but its model is also same like other two models.

Ajax call is not being sent to city/filled url and now I ended up having

http://localhost:4200/cities // useless for me
http://localhost:4200/cities/filled //useless for me

and in filled i see that ajax call is made but to the http://localhost:8000/api/cities two times, loading same data. I tried adding a dropdown on my application and opened http://localhost:4200/cities/filled in browswer, and woosh, it send ajax call to the same url 3 times, one for application.hbs and one for cities.hbs and one for city/filled. Why load the same data 3 times if it is already fetched from same url within single request?

in angular I just call a custom url and I can get the data, but for ember its really hard to get grip on these small things and there is no help around


Solution

  • active and filled are filters for your cities resource and these are not standalone resources, so you should try to use them as query parameters. like

    http://localhost:8000/api/cities?type=filled
    http://localhost:8000/api/cities?type=active
    

    Facebook uses this style for query params. You can also use query params for getting paginated data of a resource, like for 1st page and 25 records per page, the request will look like:

    http://localhost:8000/api/cities?page=1&records=25