Skip to content

Instantly share code, notes, and snippets.

@jeremywrowe
Created August 17, 2015 17:00
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeremywrowe/dbb7f4a2f5b00b5f0932 to your computer and use it in GitHub Desktop.
Save jeremywrowe/dbb7f4a2f5b00b5f0932 to your computer and use it in GitHub Desktop.
Ember Data v1.13.x Cheatsheet

Ember Data v1.13.x Cheatsheet

Things to remember

  • If you return a promise beforeModel, model, and afterModel in a route, it will wait to resolve the promise before transitioning into the route. This is helpful when using loading and error substate templates.

  • All ember data records go into a global cache. There is not a cache per query, nor is there a way to isolate that data per query. IE If you do a this.store.query("gummy-bear", { account_id: "yummy-gummy" }) and then do this.store.findAll("gummy-bear”). The findAll will send a request to the server with those params, but since there have been records loaded into the store for "gummy-bear" they will be returned with the newly fetched (scoped) records from the server.

Fetching data from the server

  • findRecord - This replaces normal find (without query params). With the default caching behavior this will first respond with what is in the cache (if applicable) and then reload the data in the background

    this.get('store').findRecord('account', 1); // => returns promise for the record defaults to { reload: false, backgroundReload: true }
  • findAll - This replaces find without an id. This is the method you should use to find a collection of a records for a given type. With the default caching behavior this will first respond with what is in the cache (if applicable) and then reload data from the server in the background.

    this.get('store').findAll('account'); // => returns promise for the records defaults to { reload: false, backgroundReload: true }
  • queryRecord - This replaces find (with query params when an id is supplied) This will always grab fresh data from the server and will not return the record from the global cache.

    this.get('store').queryRecord('account', 1, { active: true }); // => hits /accounts/1?active=true. Does not return data from the global cache
  • query - This replaces find (with query params without an id supplied) This will always grab fresh data from the server and will not include any records that are in the global cache.

    this.get('store').query('account', { active: true }); // => hits /accounts?active=true. Does not return data from the global cache

More on caching (applies to findRecord and findAll)

  • First time store.findRecord or store.findAll is called, fetch new data
  • Any time after return cached data from the global ember-data cache
  • In background, update cached data to align with server, possibly add new records (basically what does the server return). note this occurs in the background unless told otherwise.

Caching options (applies to findRecord and findAll)

  • reload: when true ember-data will go grab fresh data from the server and wait. default is false (causing a background reload)

    this.get('store').findRecord('account', 1); // => reloaded in background record1
    this.get('store').findRecord('account', 1, { reload: true }); // => waits for record1 to reload to resolve. Best way to show fresh data in the UI
  • backgroundReload: when false the data will not update in the background. this is done on settings pages where you don't want settings to be jumpy by updating data later.

    this.get('store').findRecord('account', 1); // => reloaded in background record1. Will show the record, and then will update in the UI.
    this.get('store').findRecord('account', 1, { backgroundReload: false }); // => does not update record1 in the background. Will not change in the ui until you do a findRecord again.

Fetching data from the ember data cache (will not query the server)

  • peekRecord - does not make a request to the server, returns the record if it is in the global cache, null if there is nothing in the cache

    this.get('store').peekRecord('account', 1); // => record1
    this.get('store').peekRecord('account', 2); // => null (because we have no record 2)
  • peekAll - does not make a request to the server, returns all of the records in the global cache for a given type

    this.get('store').peekAll('account'); // => [record1]
    this.get('store').peekAll('account'); // => [] (when there are no records in the global cache)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment