Access the store from a model's static (aka class-level) method?

I've got this:

Post.reopenClass({
  myStaticMethod: function() {
    // I need to access the store here!
    // this.store => undefined
  }
});

PS Why can't i just import the store or something?

Upvotes: 0

Views: 334

Answers (1)

user663031
user663031

Reputation:

Because the model class is not connected to a particular store. Stores hold instances of models, not model classes. Model instances are created from a store via store.createRecord(model..., and the resulting instances are placed in that store. So in theory, you could have instances of the same model class in different stores.

If you really want access to the store, you could do a container lookup (code smell). Or, you could arrange to pass in the store as a parameter to myStaticMethod if possible (better).

I assume perhaps you want to access the store from a static method because you're want to create a record, or find one, or something. Let's say you wanted to write a variant of createRecord which did something special. The obvious way to approach that would be to add it to the main store, or possibly put it in a subclass of DS.Store, and invoke it as store.createRecordSpecial('model', ....

Upvotes: 4

Related Questions