source: Dev/branches/rest-dojo-ui/client/rft/pages/survey.js @ 316

Last change on this file since 316 was 316, checked in by hendrikvanantwerpen, 13 years ago

Mockup included.

File size: 3.4 KB
Line 
1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/event',
2    'dojo/_base/Deferred','rft/ui/AccordionList','rft/ui/LineWithActionsWidget',
3    'rft/store','rft/ui/_Page','rft/api'],
4    function(declare,lang,event,Deferred,AccordionList,LineWithActionsWidget,store,_Page,api){
5        return declare('rft.pages.survey',[_Page],{
6            object: null,
7            postCreate: function() {
8                this.inherited(arguments);
9                this._surveyStore = store.getStore('Survey');
10                this._questionStore = store.getStore('Question');
11            },
12            onVisit: function() {
13                if ( this.pageArgs.uid ) {
14                    Deferred.when(this._surveyStore.get(this.pageArgs.uid))
15                    .then(lang.hitch(this,function(obj){
16                        this.object = obj;
17                        this.setFields(obj);
18                        return Deferred.when( obj.creator && store.dereference(obj.creator) );
19                    }))
20                    .then(lang.hitch(this,function(obj){
21                        this.creator.innerHTML = (obj && obj.email) || 'unknown';
22                    }));
23                    Deferred.when(this._questionStore.query())
24                    .then(lang.hitch(this,function(items){
25                        this._questionList = new AccordionList({
26                            store: this._surveyStore,
27                            actions: {
28                                'Add': lang.hitch(this,'_addQuestion')
29                            },
30                            idProperty: this._questionStore.idProperty,
31                            categoryProperty: 'category',
32                            titleProperty: 'title'
33                        },this.allQuestions);
34                        this._questionList.startup();
35                        this._questionList.setItems(items);
36                    }));
37                } else {
38                    this.header.innerHTML = "Error: no uid for survey!"
39                }
40            },
41            onReset: function() {
42                this.setFields(this.object);
43            },
44            onSave: function(evt) {
45                lang.mixin(this.object,this.form.get('value'));
46                Deferred.when( this._store.put(this.object) )
47                .then(lang.hitch(this,function(obj){
48                    this.object = obj;
49                    this.setFields(obj);
50                    api.notify("Object saved");
51                }),lang.hitch(this,function(){
52                    api.notify("Object save failed",'error');
53                }));
54                event.stop(evt);
55                return false;
56            },
57            setFields: function(obj) {
58                this.form.reset();
59                this.header.innerHTML = "Edit survey '"+(obj.title || '(undefined)')+"'";
60                obj && this.form.set('value',obj);
61            },
62            _addQuestion: function(obj) {
63                var d = {};
64                d.widget = new LineWithActionsWidget({
65                    title:obj.title,
66                    userObj: obj,
67                    actions:{
68                        "Remove": lang.hitch(this,'_removeQuestion',d)
69                    }
70                });
71                d.widget.placeAt(this.surveyQuestions);
72            },
73            _removeQuestion: function(data,obj) {
74                data.widget.destroy();
75            }
76        });
77    });
78
Note: See TracBrowser for help on using the repository browser.