4
\$\begingroup\$

I have ext_scaffold (question.js). When I row click I get answers on this question, so

'rowselect': function(sm, row, rec) {
    store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
        url: '/answers/index_all_for_question_id/' + rec.data.id + '?format=ext_json',
        method: 'GET'}),
        reader: new Ext.data.JsonReader({
        root: 'answers',
        id: 'id',
        totalProperty: 'results'
      }, [ 
        {name: 'text', mapping: 'answer.text'},
        {name: 'email', mapping: 'answer.respondent.email'}                             
          ])
    });

    grid = Ext.getCmp('answers_grid');            
    grid.reconfigure(store, new Ext.grid.ColumnModel([ 
                {id: 'text', header: "Answer ↓", width: 500, sortable: true, dataIndex: 'text'},          
                {id: 'email', header: "Email", width: 300, sortable: true, dataIndex: 'email'}    
          ]));
    grid.store.load();

    scaffoldPanel.selectedRecordId = rec.data.id;
    scaffoldPanel.getFormPanel().getForm().loadRecord(rec);
}

and I have another admin file where grids (answer):

 var answers_datastore = new Ext.data.Store({
     autoLoad: true,
    proxy: new Ext.data.HttpProxy({
        url: '/answers/index_all_for_question_id/<%= @questions.first.id %>?format=ext_json',
        method: 'GET'}),
    reader: new Ext.data.JsonReader({
        root: 'answers',
        id: 'id',
        totalProperty: 'results'
      },[ 
      {name: 'text', mapping: 'answer.text'},
      {name: 'email', mapping: 'answer.respondent.email'}
      ])
    });

    var answersGrid = new Ext.grid.GridPanel({
    id: 'answers_grid',
    store: answers_datastore,
            frame: true,
        columns: [
          {id: 'text', header: "Answer &darr;", width: 500, sortable: true, dataIndex: 'text'},
          {id: 'email', header: "Email", width: 300, sortable: true, dataIndex: 'email'}
        ],
         listeners: {
             rowdblclick: function(answersGrid, rowI, event)   {
            Ext.MessageBox.show({                
                title:            "Answer" ,
                   msg:             "A",
                closable:         true,
                   autowidth:        true       
           });    
             }
         },

        stripeRows: true,
        title:'Answers'
    });

How to avoid duplication?

\$\endgroup\$
1
  • \$\begingroup\$ How about something as simple as putting your duplicated values in a variable that you access from both files? \$\endgroup\$ Commented Dec 9, 2011 at 21:53

1 Answer 1

3
\$\begingroup\$

The trouble with your question is that the code resembles more config than coding and there is nothing wrong with it except that it is not DRY between those 2 files.

It seems that Ext JS supports require as of version 4, so you simply need to extract the common logic/config into a file that you will require into both places.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.