lundi 4 mai 2015

Get 1 item from all items using slug in Emberjs

I have these two models:

deomens.Artwork = DS.Model.extend({
    title: DS.attr('string'),
    slug: DS.attr('string'),
    description: DS.attr('string'),
    thumbnail: DS.attr('string'),
    artwork: DS.attr('string'),
    created_at: DS.attr('string'),
    updated_at: DS.attr('string'),
    options: DS.hasMany('option', {async: true}),
    hasOptions: Ember.computed.gt('options.length',0)
});

deomens.Option = DS.Model.extend({
    name: DS.attr('string'),
    detail: DS.attr('string'),
    artwork: DS.belongsTo('artwork')
});

I'm using RESTAdapter like so:

deomens.Store = DS.Store.extend({
    revision: 12,
    adapter: DS.RESTAdapter.create({
        namespace: 'deomens/public/api'
    })
});

deomens.ArtworkSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    options: { embedded: 'always', serialize: 'ids', deserialize: 'records' }
  }
});

Here is my router:

deomens.Router.map(function(){
    this.resource('artworks');
    this.resource('artwork', {path: '/view/:slug'});
});

And here are the two Routes I've created:

deomens.ArtworksRoute = Ember.Route.extend({
    model: function(){
        return this.store.findAll('Artwork');
    }
});

deomens.ArtworkRoute = Ember.Route.extend({
    model: function(params){
        //need to return a single item based on slug
        //something like return this.store.find('Artwork', {slug: params.slug});
    }
});

ArtworksRoute works fine - it returns all the artworks alongside with their options.

However, whenever I go to /view/artworkSlug, it fires up another GET request. So instead of doing that, can I not get an item from already retrieved data? That's one question.

The second question - let's say if I make it fire up another GET request using:

return this.store.find('Artwork', {slug: params.slug});

it sends a GET request to the following URL:

/api/artworks?slug=artworkSlug

can I not change this to something like:

/api/view/slug

instead?




Aucun commentaire:

Enregistrer un commentaire