Implement HTML module save

This commit is contained in:
Prem Sichanugrist
2012-06-28 18:10:22 -04:00
parent 0f60459709
commit 29e69db0dc
4 changed files with 22 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ from mitxmako.shortcuts import render_to_response
from xmodule.modulestore.django import modulestore
from django_future.csrf import ensure_csrf_cookie
from django.http import HttpResponse
from util.json_request import expect_json
import json
from fs.osfs import OSFS
@@ -28,6 +29,7 @@ def edit_item(request):
})
@expect_json
def save_item(request):
item_id = request.POST['id']
data = json.loads(request.POST['data'])

View File

@@ -27,6 +27,8 @@
_.extend CMS, Backbone.Events
$ ->
Backbone.emulateHTTP = true
$.ajaxSetup
headers : { 'X-CSRFToken': $.cookie 'csrftoken' }

View File

@@ -1,9 +1,17 @@
class CMS.Models.Module extends Backbone.Model
initialize: ->
url: '/save_item'
defaults:
data: '{}'
loadModule: (element) ->
try
@module = new window[@get('type')](@get('id'))
@module = new window[@get('type')](element)
catch TypeError
console.error "Unable to load #{@get('type')}." if console
editUrl: ->
"/edit_item?#{$.param(id: @get('id'))}"
save: (args...) ->
@set(data: JSON.stringify(@module.save())) if @module
super(args...)

View File

@@ -5,9 +5,16 @@ class CMS.Views.ModuleEdit extends Backbone.View
events:
'click .cancel': 'cancel'
'click .module-edit': 'editSubmodule'
'click .save-update': 'save'
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: ->
CMS.popView()