source: Dev/trunk/client/qed/pages/question.js @ 441

Last change on this file since 441 was 441, checked in by hendrikvanantwerpen, 12 years ago

Big cleanup of the question content.

  • Replaced old list implementations with a new one that behaves like a form widget.
  • All question content is now in separate widgets, not in the factory itself.
  • Added form and widget validation for question editing.
File size: 2.8 KB
Line 
1define([
2    'dojo/_base/declare',
3    'dojo/_base/event',
4    'dojo/_base/lang',
5    'dojo/when',
6    '../store',
7    '../app/Content',
8    '../app/Router',
9    '../app/Page',
10    '../model/classes/Question',
11    '../model/widgets/QuestionEditorPreview',
12    '../model/widgets/QuestionEditorToolkit',
13    'dojo/text!./templates/question.html'
14],function(declare, event, lang, when, store, Content, Router, Page, Question, QuestionEditorPreview, QuestionEditorToolkit, template){
15    return declare([Page], {
16        templateString: template,
17        question: null,
18        _toolkit: null,
19        _preview: null,
20       
21        startup: function() {
22            if ( this._started ) { return; }
23            this.inherited(arguments);
24            if ( !this.questionId ) {
25                throw new Error("Error: no reference to object set!");
26            }
27            this._setupEditor();
28            if (this.questionId === "new") {
29                this.question = Question.create();
30                this._refresh();
31            } else {
32                when(store.get(this.questionId))
33                .then(lang.hitch(this, function(obj) {
34                    this.question = obj;
35                    this._refresh();
36                }));
37            }
38        },
39        onLeave: function() {
40            this.inherited(arguments);
41        },
42        _refresh: function () {
43            this.titleNode.innerHTML = Question.DisplayTitle.get(this.question);
44            this._toolkit.set('value', this.question);
45            this._preview.set('value', Question.Content.get(this.question));
46        },
47        _onSave: function(evt) {
48            if ( this._preview.validate() ) {
49                lang.mixin(this.question, this._toolkit.get('value'));
50                Question.Content.set(this.question, this._preview.get('value'));
51                store.put(this.question)
52                .then(function() {
53                    Router.go('/questions');
54                },function(err){
55                    Content.notify(err,'error');
56                });
57            }
58            if ( evt ) { event.stop( evt ); }
59            return false;
60        },
61        _onDiscard: function() {
62            Router.go('/questions');
63            return true;
64        },
65        _setupEditor: function() {
66            this._toolkit = new QuestionEditorToolkit({
67            },this.QuestionEditorToolkitNode);
68            this._toolkit.on('submit',lang.hitch(this,"_onSave"));
69            this._toolkit.startup();
70
71            this._preview = new QuestionEditorPreview({
72                name: 'content',
73                delay: 5,
74                region: 'center'
75            });
76            this._preview.startup();
77            this.addChild(this._preview);
78        }
79    });
80});
Note: See TracBrowser for help on using the repository browser.