Nesting

As we know, ActiveJS doesn't have a single store, it has storage Units, hence instead of creating slices, we create groups.

When you need to combine multiple storage Units (or even other ActiveJS constructs) you can do that using the Cluster, to build a nested data structure, instead of storing deeply nested values in the Units.

Combining Multiple Units to create a Meaningful Cluster

To do proper nesting and to make sure that you use ActiveJS Units as intended, you should try to keep the values of the Units flat, i.e.: as less nested as possible.

This would allow you to have clear and granular control over the state of your App. Updating and accessing the stored values would be much easier and efficient as well. For example:

// instead of doing this
const store = new DictUnit({initialValue: {
  token: 'auth-token',
  userProfile: {...},
  userPreferences: {...}
}})

// do this
const tokenUnit = new StringUnit({initialValue: 'auth-token'})
const userProfileUnit = new DictUnit({initialValue: {...}})
const userPreferencesUnit = new DictUnit({initialValue: {...}})

const userDataCluster = new Cluster({
  tokenUnit, userProfileUnit, userPreferencesUnit // using shorthand notation
})

Accessing and Observing not so nested values of the Unit

Even if you followed the above advice and kept your Units flat you might still want to create an even finer chunk of the information. For that purpose, you can create a Selection from a Unit's value.

Last updated