I'm new to immutable.js and I'd like to understand better how to use records starting from a raw JS object.
With Immutable.fromJS()
I can create a map passing a raw object, for example:
var images = {
"1": {
id: "1",
urls: ["/medium/1.jpg", "/large/1.jpg"]
},
"2": {
id: "2",
urls: ["/medium/1.jpg", "/large/1.jpg"]
}
}
var imagesMap = Immutable.fromJS(images);
imagesMap
is now a map containing other maps, one for each image.
I'd like instead to create a map containing records, for example using a Image
record defined as:
var ImageRecord = Immutable.Record({ id: undefined, urls: undefined })
How can I have imagesMap
as map of ImageRecord
s? Is something I can do passing a reviver to fromJS
, or should I go with the "old" approach?
// old approach
var imagesMap = Immutable.Map()
for (key in images) {
imagesMap.set(key, new ImageRecord(images[key]))
}
js provides many Persistent Immutable data structures including: List , Stack , Map , OrderedMap , Set , OrderedSet and Record .
You can freeze (make immutable) an object using the function Object. freeze(obj) . The object passed to the freeze method will become immutable. The freeze() method also returns the same object.
ImmutableMap, as suggested by the name, is a type of Map which is immutable. It means that the content of the map are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the Map, UnsupportedOperationException is thrown.
Multiple iterations of the same Map will iterate in the same order. Map's keys can be of any type, and use Immutable.is to determine key equality.
Immutable.fromJS has an optional second parameter reviver
that you can use. You just need to decide which parts of the js object you want to turn into records.
var images = {
"1": {
id: "1",
urls: ["/medium/1.jpg", "/large/1.jpg"]
},
"2": {
id: "2",
urls: ["/medium/1.jpg", "/large/1.jpg"]
}
};
var ImageRecord = Immutable.Record({ id: "0", urls: [] })
var imagesMap = Immutable.fromJS(images, function(key, value) {
// This check should be replaced by a better way to know that
// we really want image records
if(/^[0-9]+$/.test(key)) {
return new ImageRecord(value)
}
return value
});
// Verify that it's really a record
console.log(imagesMap.get("2").toJS())
console.log(imagesMap.get("2").delete("urls").toJS())
http://jsbin.com/ronomibano/4/edit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With