Here is the code from the pre-built attendance app. I don't know javascript so it is all gobbly gook to me. I would love a way to adapt it to my own app.
Basically, from what I can tell, this inserts a new "class attendance" for every "person" connected to the class when you create a new "class day."
var app_id = Knack.app.id;
var redirect = function (total, Class, attendance_Date) {if(total < 1) {Knack.hideSpinner(); window.location.replace('#classes3/classdetails/' + Class + '/classdaydetails/' + attendance_Date)} };
$(document).on('knack-record-create.view_149', function (event, view, record) {
var user = Knack.getUserToken();
var headers = { "Authorization": user, "X-Knack-Application-ID": app_id, "Content-Type":"application/json"}
var attendance_Date = record.id;
var Class = Knack.models['view_34'].toJSON().id;
var Students = Knack.models['view_34'].toJSON().field_28_raw;
var total = Students.length;
Students.forEach(function (student) {
Knack.showSpinner();
var data = { field_72: attendance_Date, field_65: student.id };
$.ajax({
url: 'https://api.knackhq.com/v1/scenes/scene_72/views/view_145/records/',
type: 'POST',
headers: headers,
data: JSON.stringify(data),
success: function (response) {
console.log('Attendance added!!!');
total --;
redirect(total, Class, attendance_Date);
}
});
});
});
$(document).on('knack-record-create.view_141', function (event, view, record) {
var Class = $('#view_141-field_71').val();
var attendance_Date = record.id;
var user = Knack.getUserToken();
var headers = { "Authorization": user, "X-Knack-Application-ID": app_id, "Content-Type":"application/json"}
$.ajax({
url: 'https://api.knackhq.com/v1/scenes/scene_1/views/view_25/records/' + Class,
type: 'GET',
headers: headers,
success: function (data) {
Knack.showSpinner();
var Students = data.field_28_raw;
var total = Students.length;
Students.forEach(function (student) {
var data = { field_72: attendance_Date, field_31: Class, field_65: student.id };
$.ajax({
url: 'https://api.knackhq.com/v1/scenes/scene_72/views/view_145/records/',
type: 'POST',
headers: headers,
data: JSON.stringify(data),
success: function (response) {
total--
console.log('Attendance added!!!');
redirect(total, Class, attendance_Date);
}
})
});
}
});
});
$(document).on('knack-record-create.view_113', function (event, view, record) {
var Class = Knack.models['view_92'].toJSON().id;
var attendance_Date = record.id;
var Students = Knack.models['view_92'].toJSON().field_28_raw;
var total = Students.length;
var user = Knack.getUserToken();
var headers = { "Authorization": user, "X-Knack-Application-ID": app_id, "Content-Type":"application/json"}
Students.forEach(function (student) {
Knack.showSpinner();
var data = { field_72: attendance_Date, field_65: student.id };
$.ajax({
url: 'https://api.knackhq.com/v1/scenes/scene_72/views/view_145/records/',
type: 'POST',
headers: headers,
data: JSON.stringify(data),
success: function (response) {
console.log('Attendance added!!!');
total--
if(total < 1) {
Knack.hideSpinner();
window.location.replace('#classes/viewclassdetails/' + Class + '/classattendance/' + attendance_Date);
}
}
});
});
});