views:

5971

answers:

4

I have a label function like :

private function formatDate (item:Object, column:DataGridColumn):String
{
    var df:DateFormatter = new DateFormatter();
    df.formatString = "MM/DD/YY";

    if (column.dataField == "startDate") {
     return df.format(item.startDate);
    }

    return "ERR";
}

Which I use in a datacolumn by using labelFunction.

This works just fine if my data field is called 'startDate'. I want to make this function generic so I can use it everywhere.

How can I do this. i think i need to use some kind of 'reflection' - or perhaps another approach altogether?

+7  A: 

You can define another function, let's call it partial that binds some extra arguments to your function:

function partial( func : Function, ...boundArgs ) : Function {
  return function( ...dynamicArgs ) : * {
    return func.apply(null, boundArgs.concat(dynamicArgs))
  }
}

Then you change your function like this:

private function formatDate( dataField : String, item : Object, column : DataGridColumn ) : String {
  var df : DateFormatter = new DateFormatter();

  df.formatString = "MM/DD/YY";

  if ( column.dataField == dataField ) {
    return df.format(item[dataField]);
  }

  return "ERR";
}

Notice that I have added a new argument called dataField first in the argument list, and replaced all references to "startDate" with that argument.

And use it like this:

var startDateLabelFunction : Function = partial(formatDate, "startDate");
var endDateLabelFunction   : Function = partial(formatDate, "endDate");

The partial function returns a new function that calls the original function with the parameters from the call to partial concatenated with the parameters to the new function... you with me? Another way of putting it is that it can return a new function where N of the arguments are pre-bound to specific values.

Let's go through it step by step:

partial(formatDate, "startDate") returns a function that looks like this:

function( ...dynamicArgs ) : * {
  return func.apply(null, boundArgs.concat(dynamicArgs));
}

but the func and boundArgs are what you passed as arguments to partial, so you could say that it looks like this:

function( ...dynamicArgs ) : * {
  return formatDate.apply(null, ["startDate"].concat(dynamicArgs));
}

which, when it is called, will be more or less the same as this

function( item : Object, column : DataGridColumn ) : * {
  return formatDate("startDate", item, column);
}

Tada!

Theo
+1  A: 

You can make the function generic using the dataField attribute of the column as the key into your item.

private function formatDate (item:Object, column:DataGridColumn):String
{
    var df:DateFormatter = new DateFormatter();
    df.formatString = "MM/DD/YY";

    var value:object = item[column.dataField];

    return df.format(value);
}

-Ben

A: 

thnks.. Works great..

A: 

Using Ben's solution, how would you prevent format method from being called when the argument is null or empty?

Fernando
sorry im not really using AS3 anymore. good question but i'm affraid i dont have an answer. theres probably no harm though. are you just worried about performance? (btw i see you're new, but you should use comments and not the answer panel unless youre actually providing an answer or people will vote you down)
Simon_Weaver