One of my favorite features of the feature layer is that you can create one from a feature collection. There’s a sample showing how to do this. I don’t know if many people realize you can do this…so that’s one of the reasons I’ve decided to blog about it.
An example I published for the MLB Layer shows off creating a feature layer from a feature collection. The code to create a feature layer is simple:
app.fl = new esri.layers.FeatureLayer(flJson);
The key is in the flJson variable. That’s created in the fl.js file which defines a single variable from a JSON string. That variable is passed to the feature layer constructor and some JS API magic takes care of creating the feature layer.
Why would you want to do this? Well, if you have some static data (like the states in the MLB Layer example linked above), then there’s no reason to make ArcGIS Server do the work to return your features. If you’re data doesn’t change, why not just load it from a text file? If you don’t have a good answer to that question, load your feature layer up, call featureLayer.toJson, save the result to a file and serve that file as your feature layer.