What's a deepjs collider?
You saw in the previous sections that when you manipulate layers with the up or bottom tool, you can have collisions. The default behaviour is that the top layer overwrite the value (except arrays that are merged).
A deepjs collider let you manage those collisions so you can have a different behaviour than the default one.
API
- .replace( by, query )
- inject success in callback.
- .insertAt( value, index )
- inject error in callback.
- .remove( query )
- if success : inject success in doneCB. else : inject error in failCB.
- .removeAt( index, howMuch )
- if success : inject success in doneCB. else : inject error in failCB.
- .up( ... )
- inject success (if any) and error (if any) in callback.
- .bottom( ... )
- inject success (if any) and error (if any) in callback.
- .equal( needed )
- inject success (if any) and error (if any) in callback.
- .validate( schema )
- inject success (if any) and error (if any) in callback.
.replace(value)
This method set the collided property with the given value.
For example you want to override the default merging of colliding arrays by a replacement.
deep.nodes({ b:[1,2,3] })
.up({
b:deep.collider.replace([4,5])
})
.log()
.insertAt(value,position)
This method insert the "value" at the desired "position" in the array.
deep.nodes({ b:[1,2,3] })
.up({
b:deep.collider.insertAt(["hello"],2)
})
.log()
Make your own collider
You can of course build your custom colliders. This is done with the deep.collider.Collider.add() function.
Let's learn this through a simple example :
You want to use a collider that sum the numbers that collides.
deep.collider.Collider.add("sum", function(input, value){
return input + value;
});
var a = { balance:10 };
var b = {
balance:deep.collider.sum(33)
};
deep.nodes(a).up(b).log();
Colliders are chainable
You can use more than one collider by simply adding them to the chain.
example of 2 or more chained colliders