1 | define([
|
---|
2 | "../app/Content",
|
---|
3 | "../app/Page",
|
---|
4 | "../app/Router",
|
---|
5 | "../model/classes/questions",
|
---|
6 | "../model/widgets/QuestionEditorPreview",
|
---|
7 | "../model/widgets/QuestionEditorToolkit",
|
---|
8 | "../widgets/_ComplexValueMixin",
|
---|
9 | "dojo/_base/declare",
|
---|
10 | "dojo/_base/event",
|
---|
11 | "dojo/_base/lang",
|
---|
12 | "dojo/when",
|
---|
13 | "dojo/text!./templates/question.html"
|
---|
14 | ], function(Content, Page, Router, questions, QuestionEditorPreview, QuestionEditorToolkit, _ComplexValueMixin, declare, event, lang, when, template) {
|
---|
15 | return declare([Page,_ComplexValueMixin], {
|
---|
16 | templateString: template,
|
---|
17 | _toolkit: null,
|
---|
18 | _preview: null,
|
---|
19 | value: null,
|
---|
20 |
|
---|
21 | buildRendering: function() {
|
---|
22 | this.inherited(arguments);
|
---|
23 |
|
---|
24 | this._toolkit = new QuestionEditorToolkit({
|
---|
25 | },this.QuestionEditorToolkitNode);
|
---|
26 | this._toolkit.on('submit',lang.hitch(this,"_onSave"));
|
---|
27 | this._toolkit.startup();
|
---|
28 |
|
---|
29 | this._preview = new QuestionEditorPreview({
|
---|
30 | name: 'content',
|
---|
31 | delay: 5,
|
---|
32 | region: 'center'
|
---|
33 | });
|
---|
34 | this._preview.startup();
|
---|
35 | this.addChild(this._preview);
|
---|
36 | },
|
---|
37 | startup: function() {
|
---|
38 | if ( this._started ) { return; }
|
---|
39 | this.inherited(arguments);
|
---|
40 | if ( !this.questionId ) {
|
---|
41 | throw new Error("Error: no reference to object set!");
|
---|
42 | }
|
---|
43 | if (this.questionId === "new") {
|
---|
44 | this.set('value', questions.create());
|
---|
45 | } else {
|
---|
46 | when(questions.load(this.questionId))
|
---|
47 | .then(lang.hitch(this, function(value) {
|
---|
48 | this.set('value', value);
|
---|
49 | }));
|
---|
50 | }
|
---|
51 | },
|
---|
52 | _setValueAttr: function(value) {
|
---|
53 | this.value = value;
|
---|
54 | this.inherited(arguments);
|
---|
55 | this.titleNode.innerHTML = value.title || "";
|
---|
56 | },
|
---|
57 | _getValueAttr: function() {
|
---|
58 | var value = this.inherited(arguments);
|
---|
59 | lang.mixin(this.value, value);
|
---|
60 | return this.value;
|
---|
61 | },
|
---|
62 | _onSave: function(evt) {
|
---|
63 | if ( this.validate() ) {
|
---|
64 | questions.save(this.get('value'))
|
---|
65 | .then(function() {
|
---|
66 | Router.go('/questions');
|
---|
67 | },function(err){
|
---|
68 | Content.notify(err,'error');
|
---|
69 | });
|
---|
70 | }
|
---|
71 | if ( evt ) { event.stop( evt ); }
|
---|
72 | return false;
|
---|
73 | },
|
---|
74 | _onDiscard: function() {
|
---|
75 | Router.go('/questions');
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 | });
|
---|
79 | });
|
---|