简体   繁体   中英

How prevent ember-data send request to server?

I've a route which user can pick more options for your account. But when I enter on this route, ember send a GET to my API. How I avoid this since I've the information I need.

I'm using Ember JS 2x.

import Ember from 'ember';

export default Ember.Route.extend({
    searchPlaces: Ember.inject.service(),
    model() {
        return this.store.findAll('user');
    },
    afterModel(model) {
        let token = model.get('token');
        let places = model.get('places');

        this.set('places', places);
        this.set('token', token);
    }
});

First, I recommend reading the DS.Store API docs . There are four methods that seem relevant:

  • findRecord -- returns a Promise, tries memory first, then an API call
  • findAll -- like findRecord , but for all records of a type
  • peekRecord -- returns the record if it's in memory already, or null if not; doesn't make an API call
  • peekAll -- returns an Array of the records of a type that are already in memory

I suspect you want to call store.peekRecord or store.peekAll in your route if you want to avoid the API lookup.

If you access some relationships of that account in "some route" template it might need to lazy-load them and that's why GET request could fire.

If you want to get rid of that request you could override model of some route and make sure you don't use store.find there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM