views:

279

answers:

2

I grabbed this code form some book I've bumped on the InternetS...

sm: new Ext.grid.RowSelectionModel({
   singleSelect: true,
   listeners: {
       rowselect: {
           fn: function(sm,index,record) {
               Ext.Msg.alert('You Selected',record.data.title);
           }
       }
   }
});

now, sm is shorthand for selection model, we're discussing a ExtJS GridPanel here... Everything is clear until the fn: part. AFAIK, an anonymous function is passed with 3 parameters: sm, index, and record.

Right now I'm about to get down votes for asking something extremely trivial: how do you know which parameters you should pass? If I omit index parameter, I'll get an error... Why do I must pass 3 parameters? What's the catch?

+2  A: 

Parameters are not passed by name; they are passed by "position" (like in most other scripting and programming languages). The callback has to have three parameters, because the caller will provide three arguments; if there's a mismatch, an error will occur.

You
I'm aware of the fact that positional matching is being utilized here... what I don't understand is _why_ do I must pass 3 parameters: one for selection model, second one for index, and third for data value in the grid?
aL3xa
Sorry, RTFM issue... chunk from an official documentation: `rowselect : ( SelectionModel this, Number rowIndex, Ext.data.Record r )` so, an rtard finally got it: it's an event-specific function!
aL3xa
No, that's not why. The reason you must accept three parameters in your function is because extJS *expects* you to accept them. See answer by @IgorZevaka for a more thorough explanation.
You
+3  A: 

Consider this scenario:

//called with (selectionModelInstance, Index, Record)
function myCallback(sm,index,record) {
  //Parameter mapping:
  //sm -> selectionModelInstance
  //index -> Index
  //record -> Record
  alert(record.data);
  //record is actually a record object, so record.data works
}

Watch what happens when you skip a parameter:

//called with (selectionModelInstance, Index, Record)
function myCallback(sm,record) {
  //Parameter mapping:
  //sm -> selectionModelInstance
  //record -> Index
  alert(record.data); //Error
  //record is actually Index here, and it obviosly doesn't have data property.
}

The error that you seeing has nothing to do with parameter mismatch when calling a function. Javascript allows any function taking any number of parameters to be called with any number of parameters. The error is to do with trying to dereference the property record.data which is not there.

To answer you question, you must define the callback function using the signature specified by the API, simply for the sake of parameters being mapped correctly.

Igor Zevaka
Yup... it's an event-specific function, you must pass 3 parameters in an exact order, otherwise it'll throw an error! In this case, function is "linked" with the rowSelect public event.Thanks!
aL3xa
Just to clarify the previous comment, you must only pass all 3 params if you actually USE the third param. It won't throw an error because you leave off a param -- it will throw an error when you try to access a null reference. You only have to specify up to the last param that you actually want to use in a function -- all others after that one can be omitted. This applies to functions generally, not just event handlers.
bmoeskau