inertia-sails
Version:
The Sails adapter for Inertia.
48 lines (44 loc) • 1.31 kB
JavaScript
const MergeableProp = require('./mergeable-prop')
/**
* @typedef {import('../types').PropCallback} PropCallback
*/
/**
* MergeProp - A prop that merges with existing data during partial reloads.
*
* When the client requests a partial reload, merge props are combined with
* the existing data instead of replacing it entirely.
*
* @extends MergeableProp
* @example
* // Basic merge - arrays are concatenated
* notifications: sails.inertia.merge(async () => {
* return await Notification.find({ user: req.session.userId }).limit(10)
* })
*
* @example
* // Deep merge - nested objects are recursively merged
* settings: sails.inertia.merge(async () => {
* return await Settings.findOne({ user: req.session.userId })
* }).deepMerge()
*/
module.exports = class MergeProp extends MergeableProp {
/**
* Create a new MergeProp instance
* @param {PropCallback} callback - The callback function to resolve the prop value
*/
constructor(callback) {
super()
/** @type {PropCallback} */
this.callback = callback
this.merge()
}
/**
* Enable deep merging for this prop.
* Recursively merges nested objects instead of replacing them.
* @returns {MergeProp} - Returns this for chaining
*/
deepMerge() {
this.shouldDeepMerge = true
return this
}
}