3

I have got this route retrieving 2 models:

App.PanelRoute = Ember.Route.extend({
    model: function(){
    var topologymin = this.store.find('topologymin');
    var metricmap = this.store.find('metricmap', { param1: 'something'})
    return Ember.RSVP.hash({
        topologymin: topologymin,
        metricmap: metricmap
    });
 });

This makes 2 calls:

http://localhost/topologymins
http://localhost/metricmaps?param1=something

If I go to another route and again to this one, it makes again the call with the params, not the other one:

http://localhost/metricmaps?param1=something

But, as its the same call to retrieve the same records I would like them to be cached like in the other call.

How does it know when to call the server and when its not necessary? Is it possible to do that?

My models:

App.Topologymin = DS.Model.extend({
  siteGroup: DS.attr('string'),
  sites: DS.hasMany('site')
});

App.Metricmap = DS.Model.extend({
  profile: DS.attr('string'),
  link: DS.attr('string'),
  services: DS.attr()
});
Pedro4441
  • 261
  • 3
  • 14

1 Answers1

1

When you fire a request based on params Ember Data doesn't know how those params necessarily translate into the models (aka it doesn't know that you have all of the records that have some sort of relationship param1). You can cache it yourself, but then you'd still need some sort of way of knowing those records from other records in your store.

App.PanelRoute = Ember.Route.extend({
    model: function(){
      var met = this.get('fetchedBeforePromise'),
          topologymin = this.store.find('topologymin'),
          metricmap = met || this.store.find('metricmap', { param1: 'something'});

    this.set('fetchedBeforePromise', metricmap);

    return Ember.RSVP.hash({
        topologymin: topologymin,
        metricmap: metricmap
    });
 });
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • What if I am asking for more than one record? – Pedro4441 May 22 '14 at 15:26
  • I'm not exactly sure what you mean by one record, the find by query returns a collection, and In this example I'm just returning the same promise over and over which should return the same collection over and over. – Kingpin2k May 22 '14 at 15:30
  • It was a too simple example, my fault, param1 can have different values, so this wouldnt work – Pedro4441 May 22 '14 at 15:44
  • Gotcha, so how would you know you've got the records client side if param1 is changing since the server in this instance is deciding what param1 means? – Kingpin2k May 22 '14 at 15:45
  • You are right, I have open another question, maybe this is my real problem: http://stackoverflow.com/questions/23811971/change-the-way-it-calls-the-server-ember-data – Pedro4441 May 22 '14 at 16:06