source: Dev/branches/rest-dojo-ui/client/rft/pages/questions.js @ 275

Last change on this file since 275 was 275, checked in by hendrikvanantwerpen, 13 years ago
  • [Server] Added PUT support to API.
  • [Client] Extended questions page with Accordion and list with actions on each row.
File size: 3.7 KB
Line 
1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/event',
2    'dojo/_base/Deferred','dojo/store/JsonRest','dijit/layout/ContentPane',
3    'rft/ui/_Page','rft/ui/LineWithActionsWidget'],
4    function(declare,lang,array,event,Deferred,JsonRest,ContentPane,_Page,LineWithActionsWidget) {
5        return declare('rft.pages.questions',[_Page],{
6            constructor: function() {
7                this.inherited(arguments);
8                this.containers = {};
9                this.questions = {};
10            },
11            onVisit: function() {
12                this._store = new JsonRest({
13                    target:"../server/api.php/data/Question/",
14                    idProperty: 'uid'
15                });
16                this._refresh();
17            },
18            _refresh: function() {
19                Deferred.when( this._store.query() ).then(lang.hitch(this,function(results){
20                    array.forEach(results,lang.hitch(this,'_addQuestion'));
21                }));
22            },
23            _getContainerForQuestion: function(q) {
24                var category = q.category || 'Unsorted';
25                var categoryContainer = this.containers[category];
26                if ( !categoryContainer ) {
27                    var ac = new ContentPane({
28                        title:category,
29                        doLayout: false
30                    });
31                    categoryContainer = this.containers[category] = {
32                        container: ac
33                    };
34                    ac.placeAt(this.accordion);
35                    ac.startup();
36                }
37                return categoryContainer.container;
38            },
39            _addQuestion: function(q) {
40                var uid = q.uid;
41                var question = this.questions[uid];
42                if ( !question ) {
43                    var qw = new LineWithActionsWidget({
44                        title: q.title,
45                        userObject: q,
46                        actions: {
47                            'Edit': lang.hitch(this,'_editQuestion')
48                        }
49                    });
50                    qw.startup();
51                    question = this.questions[uid] = {
52                        question: q,
53                        widget: qw
54                    }
55                } else {
56                    // update info
57                    question.question = q;
58                    question.widget.title = q.title;
59                    question.widget.userObject = q;
60                    question.widget.refresh();
61                }
62                var container = this._getContainerForQuestion(q);
63                question.widget.placeAt(container.containerNode);
64            },
65            onNewQuestion: function() {
66                Deferred.when( this._store.add({}) )
67                .then(lang.hitch(this,function(question){
68                    this._editQuestion(question);
69                }));
70            },
71            _editQuestion: function(question) {
72                this.questionForm.reset();
73                this.questionForm.set('value',question);
74                this.questionDialog.show();
75            },
76            onSaveQuestion: function(evt) {
77                var value = this.questionForm.get('value');
78                Deferred.when( this._store.put(value) )
79                .then(lang.hitch(this,function(){
80                    this.questionDialog.hide();
81                    this.questionForm.reset();
82                    this._refresh();
83                }));
84                event.stop(evt);
85                return false;
86            },
87            onCancelQuestion: function() {
88                this.questionDialog.hide();
89                this.questionForm.reset();
90                this._refresh();
91            }
92        });
93    });
Note: See TracBrowser for help on using the repository browser.