views:

1547

answers:

4

ExtJS has Ext.each() function, but is there a map() also hidden somewhere?

I have tried hard, but haven't found anything that could fill this role. It seems to be something simple and trivial, that a JS library so large as Ext clearly must have.

Or when Ext really doesn't include it, what would be the best way to add it to Ext. Sure, I could just write:

Ext.map = function(arr, f) { ... };

But is this really the correct way to do this?

+1  A: 

Since map is more of a utility than anything, I don't see why there would be any special way of plugging it into the Ext namespace; the way you propose would work well enough, though you might want to do it thusly:

if(Ext && typeof(Ext.map) == "undefined") { // only if Ext exists & map isn't already defined
   Ext.map = function(arr, f) { ... };
}

Seems like that would be fine...but then, I don't use ExtJS, so I don't know. I did take a gander at their docs and it doesn't seem like there is anything special to do in this case.

Jason Bunting
+1  A: 

It appears, that my colleges here are using ext-basex, which extends Array.prototype with map() and other methods.

So I can just write:

[1, 2, 3].map( function(){ ... } );

Problem solved.

Rene Saarsoo
Over a year later, and Ext still doesn't have map(). Although they have Ext.invoke() and Ext.pluck(), which are just special cases of map(). Anyway... for now we have abandoned ext-basex, which contained too much stuff we didn't need, and written our own little library that adds map() and other useful methods to Array.
Rene Saarsoo
A: 

ExtJS doesn't replace Javascript language itself. Array functions aren't in the focus of ExtJS core. However there is a special Ext.Array object type. You can extend it on your own.

EDIT: Not Ext.Array, but just extended Array object.

Thevs
There is no Ext.Array. Ext actually extends the Array.prototype itself with indexOf() and remove() methods.
Rene Saarsoo
Yes, my mismatch in hierarchy. It is just extended Array object. But this even better for your case.
Thevs
+1  A: 

What about using one of the hybrid libraries like Ext+Prototype or Ext+Jquery. I've been using Extjs+Prototypejs for a while now and it helped me a lot to work into the Extjs code with having the more familiar prototypejs along for the ride as well.

http://extjs.com/products/extjs/build/ will build a custom tar/zip file of all the files you need to run extjs and (prototypejs|jquery|yahooUI).

David
As I sayd, I already have my problem solved by using ext-basex, but I'll keep your suggestion in mind.
Rene Saarsoo