Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4829

How to create a new instance of a model to pass to a component?

$
0
0

@alidcastano wrote:

I'm trying to create a new instance of a model so that I can pass it into a component. The component is a form that needs the model to check validations (I'm using ember-cp-validations).

I tried this:

 // route
export default Ember.Route.extend({
     model: function() {
       return this.store.createRecord('example');
     }
 });

   // template.js
{{example/create-form
              model=model
              submitForm=(action 'submit')}}

 // model
export default DS.Model.extend(Validations, {
  name: DS.attr('string'),
  problems: DS.hasMany('string')
});

But the model returns null. So even if I have the component pass the model back down along with closure action, I cannot access that same model instance to save it's properties. I have to create another new instance of the model in order to get it to work but that just feels wrong.

  submit(model, name) {
      // this.get('model').get(name)); //returns error cannot use get of null 
      // create another record
      let exampleTwo = this.get('store').createRecord('example', { name: name});
      exampleTo.save();
    }

Is there a better way to pass new instances of models along to components using ember data?

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4829

Trending Articles