Add live dashboard for teachers in the context of an exercise
This commit also adds the fundamentals for ActionCable
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
//= require turbolinks
|
||||
//= require pagedown_bootstrap
|
||||
//= require d3
|
||||
//= require rails-timeago
|
||||
//= require locales/jquery.timeago.de.js
|
||||
//
|
||||
// lib/assets
|
||||
//= require flash
|
||||
|
@@ -23,3 +23,9 @@ $.fn.scrollTo = function(selector) {
|
||||
scrollTop: $(selector).offset().top - $(this).offset().top + $(this).scrollTop()
|
||||
}, ANIMATION_DURATION);
|
||||
};
|
||||
|
||||
$.fn.replaceWithPush = function(a) {
|
||||
const $a = $(a);
|
||||
this.replaceWith($a);
|
||||
return $a;
|
||||
};
|
||||
|
13
app/assets/javascripts/cable.js
Normal file
13
app/assets/javascripts/cable.js
Normal file
@@ -0,0 +1,13 @@
|
||||
// Action Cable provides the framework to deal with WebSockets in Rails.
|
||||
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
|
||||
//
|
||||
//= require action_cable
|
||||
//= require_self
|
||||
//= require_tree ./channels
|
||||
|
||||
(function() {
|
||||
this.App || (this.App = {});
|
||||
|
||||
App.cable = ActionCable.createConsumer();
|
||||
|
||||
}).call(this);
|
33
app/assets/javascripts/channels/la_exercises.js
Normal file
33
app/assets/javascripts/channels/la_exercises.js
Normal file
@@ -0,0 +1,33 @@
|
||||
$(document).on('turbolinks:load', function() {
|
||||
if ($.isController('exercises') && $('.teacher_dashboard').isPresent()) {
|
||||
|
||||
const exercise_id = $('.teacher_dashboard').data().exerciseId;
|
||||
const study_group_id = $('.teacher_dashboard').data().studyGroupId;
|
||||
|
||||
const specific_channel = { channel: "LaExercisesChannel", exercise_id: exercise_id, study_group_id: study_group_id };
|
||||
|
||||
|
||||
App.la_exercise = App.cable.subscriptions.create(specific_channel, {
|
||||
connected: function () {
|
||||
// Called when the subscription is ready for use on the server
|
||||
},
|
||||
|
||||
disconnected: function () {
|
||||
// Called when the subscription has been terminated by the server
|
||||
},
|
||||
|
||||
received: function (data) {
|
||||
// Called when there's incoming data on the websocket for this channel
|
||||
let $row = $('tr[data-id="' + data.id + '"]');
|
||||
if ($row.length === 0) {
|
||||
$row = $($('#posted_rfcs')[0].insertRow(0));
|
||||
}
|
||||
$row = $row.replaceWithPush(data.html);
|
||||
$row.find('time').timeago();
|
||||
$row.click(function () {
|
||||
Turbolinks.visit($(this).data("href"));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user