define([ "../app/Content", "../app/Page", "../app/Router", "../model/classes/Question", "../model/widgets/QuestionEditorPreview", "../model/widgets/QuestionEditorToolkit", "../store", "../widgets/_ComplexValueMixin", "dojo/_base/declare", "dojo/_base/event", "dojo/_base/lang", "dojo/when", "dojo/text!./templates/question.html" ], function(Content, Page, Router, Question, QuestionEditorPreview, QuestionEditorToolkit, store, _ComplexValueMixin, declare, event, lang, when, template) { return declare([Page,_ComplexValueMixin], { templateString: template, _toolkit: null, _preview: null, value: null, buildRendering: function() { this.inherited(arguments); this._toolkit = new QuestionEditorToolkit({ },this.QuestionEditorToolkitNode); this._toolkit.on('submit',lang.hitch(this,"_onSave")); this._toolkit.startup(); this._preview = new QuestionEditorPreview({ name: 'content', delay: 5, region: 'center' }); this._preview.startup(); this.addChild(this._preview); }, startup: function() { if ( this._started ) { return; } this.inherited(arguments); if ( !this.questionId ) { throw new Error("Error: no reference to object set!"); } if (this.questionId === "new") { this.set('value', Question.create()); } else { when(store.get(this.questionId)) .then(lang.hitch(this, function(value) { this.set('value', value); })); } }, _setValueAttr: function(value) { this.value = value; this.inherited(arguments); this.titleNode.innerHTML = Question.DisplayTitle.get(value); }, _getValueAttr: function() { var value = this.inherited(arguments); lang.mixin(this.value, value); return this.value; }, _onSave: function(evt) { if ( this.validate() ) { var value = this.get('value'); store.put(value) .then(function() { Router.go('/questions'); },function(err){ Content.notify(err,'error'); }); } if ( evt ) { event.stop( evt ); } return false; }, _onDiscard: function() { Router.go('/questions'); return true; } }); });