lundi 6 novembre 2017

Ember: Dynamically add a record to array

I have a page where I want to let user enter multiple records. For example, let's say I have a page where user can enter as many as Student record he wants, where each Student record contains fields like name, age, etc.

Currently, I have a button on the page which calls a method on a controller.

// controller/studentlist.js
actions: {
    createStudentRecord() {
        let student =  this.get('store').createRecord('student', {});
        let model = this.get('model') || Ember.A();
        model.pushObject(student);
        this.set('model', model);
    }
}

I initially fetch the model like this:

// routes/studentlist.js
model(params) {
    return this.get('store').query('student', { orderBy: 'class_id', equalTo: params.class_id });
}

Then, I iterate over the model in my template so when the model gets updated, the list also gets updated.

// templates/studentlist.hbs

    // Show list of students

    <p>No students.</p>


However, when I do this, I get internalModel.getRecord is not a function.

Googling lead to this on github. http://ift.tt/2fXQxJx, which seems to be the exact same issue I have. However, I don't quite get what I am supposed to do to get this scenario working. Could someone guide me to properly implement this?




Aucun commentaire:

Enregistrer un commentaire