Ember Data: create a model without a store

Is there a way to create an object DS.Modelwithout using store.createRecord?

EDIT

Maybe I need to give some context.

I am writing Ember Addon, which has several modelthat do not intersect through the app / directory , and I want to write unit tests for these model. In unit tests with an autogenerator, an auxiliary one is used moduleForModel, which works fine if modelconnected. But since mine are modelsnot connected, they are not combined into the namespace of fictitious applications, and the moduleForModelhelper cannot find model.

That's why I wanted to create modelwithout using an object store, because I could not find a way to access storewithout using a moduleForModelhelper.

+4
source share
2 answers

No. DS.Modelis Ember.Object, therefore, if possible, we would use create:

const User = DS.Model.extend({
  firstName:   DS.attr('string'),
  lastName:    DS.attr('string'),
});

const foo = User.create({ firstName: 'Foo', lastName: 'Bar' });

> Uncaught EmberError { message: "You should not call `create` on a model. Instead, call `store.createRecord` with the attributes you would like to set.", ... ]

DS.Model instances are attached to the repository by design (that they receive updates when new data is entered), so it makes no sense to create an instance DS.Modelwithout this connection. Ember Data makes this explicit by requiring you to call createRecordinto your store instance.

+1
source

, . javascript ( , ) . Ember hash. guide . , hash new dirty. root.loaded.saved.

, .

0

Source: https://habr.com/ru/post/1649209/


All Articles