-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from supercharge/view-share-data
Views: share data
- Loading branch information
Showing
9 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
import { ViewSharedData } from '@supercharge/contracts' | ||
|
||
export class ViewBaseCompiler { | ||
/** | ||
* Stores the data that is available to all view templates. | ||
*/ | ||
private state: Record<string, any> = {} | ||
|
||
/** | ||
* Share a given state of data to all views, across HTTP requests. | ||
* | ||
* @example | ||
* ``` | ||
* import { View } from '@supercharge/facades' | ||
* | ||
* View.share({ key: 'value' }) | ||
* ``` | ||
*/ | ||
share<K extends keyof ViewSharedData> (key: K, value: ViewSharedData[K]): this | ||
share (values: Partial<ViewSharedData>): this | ||
share (key: string | any, value?: any): this { | ||
if (this.isObject(key)) { | ||
this.state = { ...this.state, ...key } | ||
} else if (typeof key === 'string') { | ||
this.state[key] = value | ||
} else { | ||
throw new Error(`Failed to set shared view state: the first argument is neither a string nor an object. Received "${typeof key}"`) | ||
} | ||
|
||
return this | ||
} | ||
|
||
/** | ||
* Returns the shared data. | ||
*/ | ||
sharedData (): Record<string, any> { | ||
return this.state | ||
} | ||
|
||
/** | ||
* Determine whether the given `input` is an object. | ||
*/ | ||
private isObject (input: any): input is Record<string, any> { | ||
return !!input && input.constructor.name === 'Object' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
export { HandlebarsCompiler } from './engines/index.js' | ||
export { ViewManager } from './view-manager.js' | ||
export { ViewConfigBuilder } from './view-config-builder.js' | ||
export { View } from './view-response.js' | ||
export { ViewManager } from './view-manager.js' | ||
export { ViewResponse } from './view-response.js' | ||
export { ViewServiceProvider } from './view-service-provider.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters