DS#inject

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

Inject the given item into the data store for the specified resource. If attrs is an array, inject each item into the data store. Injecting an item into the data store does not save it to the server. Calls the beforeInject and afterInject hooks. Emits the DS.beforeInject and DS.afterInject events. Any item you inject must have a primary key.

Returns the inject item(s).

πŸ‘

You can call DS#inject multiple ways

  • DS#inject(resourceName, attrs[, options])
  • Resource#inject(attrs[, options]) - Where Resource was created by DS#defineResource
ArgumentTypeDescription
resourceNamestringThe name of the resource to use. Unnecessary if calling inject directly on a Resource.
attrsobject or array of objectsItem or items to inject. Any item you inject must have a primary key.
options (optional)objectSettings are inherited from Resource and Global defaults.
options.onConflictstringWhat to do when injecting an item and an item with the same primary key already exists in the store. Default is merge. The other option is replace.
options.notifybooleanWhether to emit the DS.beforeInject and DS.afterInject events. Default true.
Examples
Document.get(45); // undefined

Document.inject({ title: 'How to Cook', id: 45 });

Document.get(45); // { title: 'How to Cook', id: 45 }

Inject a collection into the data store:

Document.filter(); // [ ]

// See Method Variants section below for different ways to call DS#inject
Document.inject([ { title: 'How to Cook', id: 45 }, { title: 'How to Eat', id: 46 } ]);

Document.filter(); // [ { title: 'How to Cook', id: 45 }, { title: 'How to Eat', id: 46 } ]

πŸ“˜

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.