samedi 24 décembre 2016

EmberJs Data Goes Null to Server

I am building a toy app and wanted to work with a simple 1:many relationship. My car model "hasMany comments" & my comments "belongsTo car".I want a comment to be created from cars/car route.So i didn't created a route for comments/new.For a quick trial i have added a button that has "saveComment" action.

<button type="submit" >Submit</button>

!!! I am passing the model(which refers to current Car object) because in my action i want to read it's id and set it to comments car_id.

And here is the correspanding route file

  model(params){
   return this.store.findRecord('car', params.car_id);
  },
 actions: {
  saveComment(model){
     let car = this.store.peekRecord('car', model.id),
     let newComment = this.store.createRecord('comment'{
            car: car,
            body: 'Some filler text! ',
        });
   car.get('comments').pushObject('newComment');
   newComment.save();
  }

The problem is, my (let the route be cars/10) current template includes the newly generated comment with the proper body, however when i inspect it, belongsTo relationship looks empty.Since car_id goes null, after a reload, new generated comments disappeares.(not from the database tho, it is saved succesfully without a car relationship).What should i do to get this comments.car saved? TIA




Aucun commentaire:

Enregistrer un commentaire