I’m trying to get my head around Ember Data by creating a simple attendance app. The goal is to have a student type in their name and hit submit, creating a new Attendance record that is associated with that student. Here’s what I’ve done to make it work:
App.Student = DS.Model.extend({
name: DS.attr('string'),
attendances: DS.hasMany('attendance', { async: true })
});
App.Attendance = DS.Model.extend({
student: DS.belongsTo('student', { async: true }),
...
});
App.NewAttendanceController = Ember.ObjectController.extend({
content: {},
actions: {
createAttendance: function() {
var studentName = this.get('name');
this.store.find('student', { name: studentName })
.then(function(fulfilledPromise) {
var student = fulfilledPromise.get('firstObject');
var attendance = fulfilledPromise.store.createRecord('attendance', {
time: moment(),
student: student
});
attendance.save();
});
}
}
});
So this works, but it feels really strange, especially everything in the .then()
callback. I also found out that the scope in .then()
is completely different from what I expected (I don’t have access to any variables above, for instance). Anyway, any insight would be much appreciated. Thanks!