7
votes

I did common modal pop-up for one of the project. For that modal pop-up, I am passing data-model(say Salary model) as a parameter. Sometimes we need to pass model as an object (i.e. instead of salary model we pass salary object). Based on that i am checking if parameter is an instance of ember or an object .

My question is, can we convert object(say salary object) to ember data model (say salary model)?

For eg:- I have a Model like below

App.Salary=DS.Model.extend({
emp_name:DS.attr('string'),
emp_salary:DS.attr('string')
});

Json object
{salary:{id:1,emp_name:'Raju',emp_salary:'5000'}}

For some reasons , I pass ember salary model as an parameter / salary object(JSON) as an parameter

Both are having same data , but salary model will be ember instance . If i change some thing in node , it will reflect in associated models. But for salary object , if change some thing in node , it will not reflect in associated models .

I know salary object is not associated with ember-data model , that's why it will not reflect with salary associated models .

So is there any way to convert that salary object into salary model . So if i change something in the node , it will reflect associated models .

3
Would you mind setting up a more concrete example, it's difficult to give a good answer without more details. As it stands, sure you can sideload a pojo into the store and create an ember data model. (for future reference, emberjs.jsbin.com is a great place to create an example)Kingpin2k
@kingpin2k,I edited my questionmaheshiv

3 Answers

3
votes

We can use

this.store.push(this.store.normalize('salary', {id:1,emp_name:'Raju',emp_salary:'5000'}));
0
votes

Try setting that JSON object as an Ember object instead:

Em.Object.create({salary:{id:1,emp_name:'Raju',emp_salary:'5000'}})
0
votes

For me this did the trick:

this.get('store').createRecord('model', {payload})