@soanvig wrote:
Hi there!
Today I began my journey with EmberJS.
Been through tutorial and decided to create Spotify-like app (using Spotify API of course!).Basically it works (however the API doesn't follow any of Ember rules). I decided to create search stuff.
Spotify API for searching looks like this:
https://api.spotify.com/v1/search?q=irfan&type=artist
Where q= is the question, and type is arist, track or album (or each of these, separated).Just for training purposes I am working on query noted above.
Okey, can You see that JSON response? It's a Hash of search types (artists, tracks, albums as result). In each of hash there is items array.
My first thought was: hey, let's create RESTAdapter + RESTSerializer, and model for each of the types: artist.js, track.js, album.js.
[I have created only artist.js because no track nor album in response).
Then just move array of items directly into artists hash, to meet convention.First step is obvious, and nothing can be wrong here.
The second step I did:
serializers/search.jsimport DS from 'ember-data'; export default DS.RESTSerializer.extend({ normalizeResponse(store, primaryModelClass, payload, id, requestType) { for(let key in payload) { payload[key] = payload[key].items; } return this._super(store, primaryModelClass, payload, id, requestType) } });
(of course after hours of trying). Done.
Now, Ember Inspector says, that I got X (20) results in "artists" store, and... well, 0 results in search store.
WARN: artists model matches hash the API returns. However search model is empty (no fields), because I had no idea what should be there.
Okey, so... How should I show search results to the user? Of course I can dump all records from artists, but after few searches there will too much results (previous results of course won't be removed).
The simplest expected result: after executing query
this.get('store').queryRecord('search', {q: val, type: 'artist'});
NOTE: queryRecord, because for query I got error, that in result I get one record, not multiple.
I should get hash-like model with result type as keys, and array of related model-object items i.e.
{ 'artists' : [ artistobject1, artistobject2, artistobject3 ] }
But I have no idea how to achieve that, and why I get no Search model result, only Artist gets populated. I have tried to do something with promise returned by query (.then -> function(data) -> console.log(data) but I got back null)
Posts: 1
Participants: 1