Reputation: 3829
A few of my model have property with a list of state :
thing.js
...
status: DS.attr('number'),
statesValues: {1 : "First",2: "Some Choice", 3: "Nothing"}
...
How can I in another ember object, access the values from the Thing model ? I tried to Import the model but I do not even know the correct path to import.
Upvotes: 0
Views: 218
Reputation: 164
I think the service will be good solution to share the static data across an Ember.js application and inject it whenever you need.
// app/services/static-data.js
import Service from '@ember/service';
import { computed } from '@ember/object'
export default Service.extend({
statesValues: computed(function() {
return { 1: 'First', 2: 'Second', 3: 'Third' };
})
});
then you can inject it in any places of you application, components, models, controllers, etc...
// app/models/thing.js
import DS from 'ember-data';
import { inject as service } from '@ember/service';
export default DS.Model.extend({
staticData: service()
....
});
Upvotes: 1
Reputation: 3829
Finally I added this in my model :
export const statusValues ={...}
And to use it i import it as needed :
import {statusValues} from 'app-name/models/model-name'
Upvotes: 0
Reputation: 2286
You need to fetch the model from the store using one of the methods written about here: https://guides.emberjs.com/release/models/finding-records/.
For an existing record
const myModel = await this.store.findRecord('thing', 1);
console.log(myModel.get('statesValues'));
For new record
const myModel = this.store.createRecord('thing', { /* optional defaults go here, e.g., `status: 2,` */ });
console.log(myModel.get('statesValues'));
Upvotes: 0