source: Dev/branches/rest-dojo-ui/client/rft/pages/viewSurvey.js @ 417

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

Put all model dependent code in one place. More separation of general and domain code.

File size: 2.5 KB
Line 
1define([
2    'dojo/_base/array',
3    'dojo/_base/declare',
4    'dojo/_base/Deferred',
5    'dojo/_base/event',
6    'dojo/_base/lang',
7    '../store',
8    '../app/Page',
9    '../model/widgets/QuestionWidgetFactory',
10    'dojo/text!./templates/viewSurvey.html'
11],function(array,declare,Deferred,event,lang,store,Page,QuestionWidgetFactory,template){
12    return declare([Page],{
13        templateString: template,
14        survey: null,
15        surveyId: "",
16        options: null,
17        constructor: function(){
18            this._dataMap = {};
19            this.options = this.options || {};
20        },
21        startup: function() {
22            if ( this._started ) { return; }
23            this.inherited(arguments);
24
25
26            if ( this.surveyId ) {
27                Deferred.when(store.get(this.surveyId))
28                .then(lang.hitch(this,function(survey){
29                    if ( !survey.published ) {
30                        this.options.preview = true;
31                    }
32                    if ( this.options.preview ) {
33                        this.buttonsPane.destroyRecursive();
34                    }
35                    this.titleNode.innerHTML = survey.title +
36                            (this.options.preview?' [preview]':'');
37                    var f = new QuestionWidgetFactory();
38                    this.survey = survey;
39                    store.query(null,{keys:this.survey.questions,include_docs:true})
40                    .forEach(function(question){
41                        array.forEach(question.content || [],function(item){
42                            var w = f.createViewWidget(item,{
43                                name: question.code+'.'+item.code
44                            });
45                            if ( w !== null ) {
46                                w.placeAt(this.questionsAnchor,'before');
47                            }
48                        },this);
49                    },this);
50                }));
51            } else {
52                throw new Error("No valid uid or survey passed!");
53            }
54        },
55        _onSubmit: function(evt) {
56            if ( this.options.preview ) { return; }
57            var value = this.questionsForm.get('value');
58            this.questionsPane.set('content','<pre>'+JSON.stringify(value)+'</pre>');
59            event.stop(evt);
60            return false;
61        },
62        _onCancel: function(evt) {
63            if ( this.options.preview ) { return; }
64            event.stop(evt);
65            return false;
66        }
67    });
68});
Note: See TracBrowser for help on using the repository browser.