Модель маршрута EmberJS отправляет вызов API каждый раз, когда запрашивается маршрут

У меня есть маршрут, который требует загрузки набора моделей через вызовы API. Все работает нормально, но всякий раз, когда я перехожу на другой маршрут, а затем возвращаюсь к маршруту, Эмбер запускает еще один набор запросов. Как я могу кэшировать результат при первой загрузке маршрута? Код доступен здесь: https://github.com/knusul/embercv/blob/master/app/assets/javascripts/routes/index_route.coffee

App.IndexRoute = Ember.Route.extend
  model: (param)->
    return Em.RSVP.hash(
      card: @store.find('card', 'singleton')
      experiences: @store.find('experience')
      educations: @store.find('education')
      skills: @store.find('skill')
      languages: @store.find('language')
      hobbies: @store.find('hobby')
    ).then (hash) ->
      return Em.RSVP.hash(hash)

  setupController: (controller, model)->
    if App.currentUser
      @controllerFor('card').set 'model', model.card
      @controllerFor('experiences').set 'model', model.experiences
      @controllerFor('educations').set 'model', model.educations
      @controllerFor('skills').set 'model', model.skills
      @controllerFor('languages').set 'model', model.languages

person Jakub Nieznalski    schedule 24.11.2013    source источник


Ответы (2)



Когда вы выполняете хотя бы один find('myModel'), полученные данные сохраняются в кэше записей, и к ним можно получить доступ с помощью all('myModel') без новых запросов. Таким образом, вы можете обновить свой код следующим образом:

App.ApplicationRoute = Ember.Route.extend
  # the model method is called once, when app start
  model: (param)->
    return Em.RSVP.hash(
      card: @store.find('card', 'singleton')
      experiences: @store.find('experience')
      educations: @store.find('education')
      skills: @store.find('skill')
      languages: @store.find('language')
      hobbies: @store.find('hobby')
    ).then (hash) ->
      return Em.RSVP.hash(hash)

App.IndexRoute = Ember.Route.extend
  # because we already loaded all data in application route
  # just use all to access the record cache without new ajax requests
  model: (param)->
    return Em.RSVP.hash(
      card: @store.all('card', 'singleton')
      experiences: @store.all('experience')
      educations: @store.all('education')
      skills: @store.all('skill')
      languages: @store.all('language')
      hobbies: @store.all('hobby')
    ).then (hash) ->
      return Em.RSVP.hash(hash)

  setupController: (controller, model)->
    if App.currentUser
      @controllerFor('card').set 'model', model.card
      @controllerFor('experiences').set 'model', model.experiences
      @controllerFor('educations').set 'model', model.educations
      @controllerFor('skills').set 'model', model.skills
      @controllerFor('languages').set 'model', model.languages
person Marcio Junior    schedule 24.11.2013