DS#create

DS#create(resourceName, attrs[, options])

The "C" in "CRUD", create is for creating a single item via an adapter. With completely default settings, it's probably going to end up making a POST request to /resource, where resource is the name of the resource, e.g. store.create('user', { name: 'John' }). If you're working with the resource directly you can just do User.create({ name: 'John' }).

create asynchronous and returns a promise.

Delegates to the create method of whichever adapter is being used and then injects the created item into the data store.

cacheResponse: false will cause the result to not be injected into the store.

As create delegates to an adapter, the options argument (if you passed one) will also be passed to the adapter's create method, so you can pass options to the adapter as well.

If the result is to be injected into the store, the options argument will also be passed to DS#inject when it is called.

πŸ‘

You can call DS#create multiple ways

  • DS#create(resourceName, attrs[, options])
  • Resource#create(attrs[, options]) - Where Resource was created by DS#defineResource
  • Instance#DSCreate([options]) - Where Instance is an unsaved instance of a Resource.
ArgumentTypeDescription
resourceNamestringThe name of the resource to use. Unnecessary if calling create directly on a Resource.
attrsobjectThe properties with which to create the item.
options (optional)objectSettings are inherited from Resource and Global defaults. Will be passed through to the adapter's create method and DS#inject, if it is called.
options.adapterstringThe name of a registered adapter to use.
options.cacheResponsebooleanInject the updated item into the store.
options.upsertbooleanIf attrs already contains a primary key, then attempt to call DS#update instead.
Because options is passed through to an adapter's create method or DS#inject, then the options for those methods are valid options here as well.
Live Demo

πŸ“˜

Need help?

Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.