Commit 29e69db0 by Prem Sichanugrist

Implement HTML module save

parent 0f604597
...@@ -2,6 +2,7 @@ from mitxmako.shortcuts import render_to_response ...@@ -2,6 +2,7 @@ from mitxmako.shortcuts import render_to_response
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
from django_future.csrf import ensure_csrf_cookie from django_future.csrf import ensure_csrf_cookie
from django.http import HttpResponse from django.http import HttpResponse
from util.json_request import expect_json
import json import json
from fs.osfs import OSFS from fs.osfs import OSFS
...@@ -28,6 +29,7 @@ def edit_item(request): ...@@ -28,6 +29,7 @@ def edit_item(request):
}) })
@expect_json
def save_item(request): def save_item(request):
item_id = request.POST['id'] item_id = request.POST['id']
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
_.extend CMS, Backbone.Events _.extend CMS, Backbone.Events
$ -> $ ->
Backbone.emulateHTTP = true
$.ajaxSetup $.ajaxSetup
headers : { 'X-CSRFToken': $.cookie 'csrftoken' } headers : { 'X-CSRFToken': $.cookie 'csrftoken' }
......
class CMS.Models.Module extends Backbone.Model class CMS.Models.Module extends Backbone.Model
initialize: -> url: '/save_item'
defaults:
data: '{}'
loadModule: (element) ->
try try
@module = new window[@get('type')](@get('id')) @module = new window[@get('type')](element)
catch TypeError catch TypeError
console.error "Unable to load #{@get('type')}." if console console.error "Unable to load #{@get('type')}." if console
editUrl: -> editUrl: ->
"/edit_item?#{$.param(id: @get('id'))}" "/edit_item?#{$.param(id: @get('id'))}"
save: (args...) ->
@set(data: JSON.stringify(@module.save())) if @module
super(args...)
...@@ -5,9 +5,16 @@ class CMS.Views.ModuleEdit extends Backbone.View ...@@ -5,9 +5,16 @@ class CMS.Views.ModuleEdit extends Backbone.View
events: events:
'click .cancel': 'cancel' 'click .cancel': 'cancel'
'click .module-edit': 'editSubmodule' 'click .module-edit': 'editSubmodule'
'click .save-update': 'save'
initialize: -> initialize: ->
@$el.append($("""<div id="#{@model.get('id')}">""").load(@model.editUrl())) @$el.load @model.editUrl(), =>
@model.loadModule(@el)
save: (event) ->
event.preventDefault()
@model.save().success ->
console.log "Saved"
cancel: -> cancel: ->
CMS.popView() CMS.popView()
......
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