Commit e1fd6d73 by Victor Shnayder

refactor to be more clearly model-view

parent b1d5273a
...@@ -25,6 +25,7 @@ class StaffGradingBackend ...@@ -25,6 +25,7 @@ class StaffGradingBackend
rubric: 'A rubric!' + @mock_cnt rubric: 'A rubric!' + @mock_cnt
else if cmd == 'save_grade' else if cmd == 'save_grade'
console.log("eval: #{data.score} pts, Feedback: #{data.feedback}")
response = response =
success: true success: true
submission: 'another submission! ' + @mock_cnt submission: 'another submission! ' + @mock_cnt
...@@ -60,6 +61,7 @@ class StaffGrading ...@@ -60,6 +61,7 @@ class StaffGrading
constructor: (backend) -> constructor: (backend) ->
@backend = backend @backend = backend
# all the jquery selectors
@error_container = $('.error-container') @error_container = $('.error-container')
@message_container = $('.message-container') @message_container = $('.message-container')
@submission_container = $('.submission-container') @submission_container = $('.submission-container')
...@@ -67,75 +69,98 @@ class StaffGrading ...@@ -67,75 +69,98 @@ class StaffGrading
@submission_wrapper = $('.submission-wrapper') @submission_wrapper = $('.submission-wrapper')
@rubric_wrapper = $('.rubric-wrapper') @rubric_wrapper = $('.rubric-wrapper')
@button = $('.submit-button') @button = $('.submit-button')
@button.click @clicked
# model state
@state = state_no_data @state = state_no_data
@submission = ''
@rubric = ''
@error_msg = ''
@message = ''
@feedback = null
@score = null
# action handlers
@button.click @clicked
@submission_wrapper.hide() # render intial state
@rubric_wrapper.hide() @render_view()
# send initial request automatically
@get_next_submission() @get_next_submission()
set_button_text: (text) -> set_button_text: (text) ->
@button.prop('value', text) @button.prop('value', text)
ajax_callback: (response) => ajax_callback: (response) =>
if response.success # always clear out errors and messages on transition.
if response.submission @error_msg = ''
@data_loaded(response.submission, response.rubric) @message = ''
else
@no_more() if response.success
if response.submission
@data_loaded(response.submission, response.rubric)
else else
@error(response.error) @no_more()
else
@error(response.error)
@render_view()
get_next_submission: () -> get_next_submission: () ->
@backend.post('get_next', {}, @ajax_callback) @backend.post('get_next', {}, @ajax_callback)
submit_and_get_next: () -> submit_and_get_next: () ->
data = {eval: '123'} data = {score: '1', feedback: 'Great!'}
@backend.post('save_grade', data, @ajax_callback) @backend.post('save_grade', data, @ajax_callback)
error: (msg) -> error: (msg) ->
@error_container.html(msg) @error_msg = msg
@state = state_error @state = state_error
@update()
data_loaded: (submission, rubric) -> data_loaded: (submission, rubric) ->
@submission_container.html(submission) @submission = submission
@rubric_container.html(rubric) @rubric = rubric
@state = state_grading @state = state_grading
@update()
no_more: () -> no_more: () ->
@submission = null
@rubric = null
@message = 'Nothing to grade'
@state = state_no_data @state = state_no_data
@update()
update: () -> render_view: () ->
# make button and div state match the state. Idempotent. # make the view elements match the state. Idempotent.
show_grading_elements = false
@message_container.html(@message)
@error_container.html(@error_msg)
if @state == state_error if @state == state_error
@set_button_text('Try loading again') @set_button_text('Try loading again')
else if @state == state_grading else if @state == state_grading
@submission_wrapper.show() @submission_container.html(@submission)
@rubric_wrapper.show() @rubric_container.html(@rubric)
show_grading_elements = true
@set_button_text('Submit') @set_button_text('Submit')
else if @state == state_no_data else if @state == state_no_data
@submission_wrapper.hide() @message_container.html(@message)
@rubric_wrapper.hide()
@message_container.html('Nothing to grade')
@set_button_text('Re-check for submissions') @set_button_text('Re-check for submissions')
else else
@error('System got into invalid state ' + @state) @error('System got into invalid state ' + @state)
@submission_wrapper.toggle(show_grading_elements)
@rubric_wrapper.toggle(show_grading_elements)
clicked: (event) => clicked: (event) =>
event.preventDefault() event.preventDefault()
# always clear out errors and messages on transition...
@message_container.html('')
@error_container.html('')
if @state == state_error if @state == state_error
@get_next_submission() @get_next_submission()
else if @state == state_grading else if @state == state_grading
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
<h3>Rubric</h3> <h3>Rubric</h3>
<div class="rubric-container"> <div class="rubric-container">
</div> </div>
</section> </section>
<div class="submission"> <div class="submission">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment