Ember CreateRecord


#1

I am creating record with createRecord() method . after save(). current template dose not show new value.

here is my code : actions: { addLineItems(line_item) { var item = this.get(‘model’);

var item = this.store.createRecord('invoice-line-item');

item.save();
this.store.push(item);

},

remove: function (line_item){ // console.log(line_item); // line_item.deleteRecord(); // line_item.save(); var item = this.get(‘model’) item.deleteRecord(line_item); }


#2

You shouldn’t need to push the record into the store after creating it (createRecord creates it in the store already).

My guess is that the issue has something to do with your usage of ‘model’.

var item = this.get(‘model’);

Here you’re getting an item from your model (on controller probably?) and storing it in a var called item, then you’re immediately overwriting item with a new record and saving the record. Of course you’re never overwriting changing model and my guess is you are referencing model in your template, which is probably why your template never updates.


#3

yes i referring model in template . as soon as new record created i want to show on template without refreshing page . here is new code


#4

I think you need to use pushObject and removeObject as the native array functions are not KVO complient.

actions: {
  addLineItem () {
    let model = this.get('model');
    let invoice = this.get('model.invoice');
    this.set('error', null);
    this.set('isLoading', true);
    this.store.createRecord('invoice-line-item')
      .save()
      .then(item => invoice.pushObject(item))
      .then(() => model.save())
      .catch(error => this.set('error', error))
      .finally(() => this.set('isLoading', false);
  },

  remove(lineItem) {
    let model = this.get('model');
    let invoice = this.get('model.invoice');
    invoice.removeObject(lineItem);
    this.set('error', null);
    this.set('isLoading', true);
    model.save()
      .catch(error => this.set('error', error))
      .finally(() => this.set('isLoading', false);
  }
}

#5

i have tried this but i did not get updated list without refreshing page . & for remove action i got error => Uncaught TypeError: Cannot read property ‘removeObject’ of undefined


#6

here is my code


#7

model code


#8


#9

What’s in your model hook? Your code just didn’t make sense.

Or you should try to make a full example using https://ember-twiddle.com/ . It will help other people understand your problem.