Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically populate data properties with passed parameters when onMount() isn't defined #131

Closed
mrigsby opened this issue Oct 25, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@mrigsby
Copy link
Contributor

mrigsby commented Oct 25, 2023

I ran into an issue the other day when I passed parameters to a wire and neglected to add an onMount() event. The wire threw an error about not having a setter for my parameter.

I was looking at how cbwire handles onMount() in /models/concerns/OnMountConcern.cfc and was thinking about how this might be handled differently.

Building on box products' love of conventions, I was thinking about an option where if onMount() does not exist, it checks that each parameter is a string, boolean, numeric, date, array, or struct and if they are cbwire populates the component using scope population instead of setters.

This would allow us to skip the onMount() function if we want to just merge basic parameters directly into the data property.

@grantcopley, am I missing something else this might affect? I know there might be a minor hit in performance if passing in a large number of parameters however, this can be negated by just adding the onMount() function and manually handling parameters.

// check datatypes parameters and throw error if not string, boolean, numeric, date, array, or struct
for( var paramKey IN arguments.parameters.keyArray() ){
	if(     !isSimpleValue( arguments.parameters[ paramKey ] ) 
		&& !isArray( arguments.parameters[ paramKey ] ) 
		&& !isStruct( arguments.parameters[ paramKey ] )
	){
		throw( 
			type 	= "MissingOnMount", 
			message = "The wire does NOT have an onMount() function and the paramaters passed contain a 
				data type other than string, boolean, numeric, date, array, or struct and cannot 
				be automatically inserted into the data properties. To avoid this error create an 
				onMount() method to handle incoming paramaters" 
		);
	}
}

// if parameters passed in are string, boolean, numeric, date, array, or struct insert into variables.data
getPopulator().populateFromStruct(
	target: comp.getParent(),
	memento: arguments.parameters,
	scope: "variables.data"
);
@grantcopley
Copy link
Collaborator

@mrigsby Good idea! I never thought about handling the params automatically. I am surprised that it threw an error if you didn't define an onMount() method. Could you include the error here?

As far as a feature though, let's add it. We can get it added and target it for the 3.2 release.

@grantcopley
Copy link
Collaborator

@mrigsby OK I see it now in https://github.com/coldbox-modules/cbwire/blob/development/models/concerns/OnMountConcern.cfc. In my testing, I've just always defined onMount() and therefore never ran into the ELSE block. Definitely makes sense now why it errored.

} else {
			/**
			 * Use setter population to populate our component.
			 */
			getPopulator().populateFromStruct(
				target: this,
				trustedSetter: true,
				memento: arguments.parameters,
				excludes: ""
			);
		}

@mrigsby
Copy link
Contributor Author

mrigsby commented Oct 25, 2023

Awesome, I just submitted a pull request! Let me see if I can recreate the error. I believe the culprit is that /models/concerns/OnMountConcern.cfc runs populateFromStruct() if there is no onMount() and uses trustedSetter = true which calls the setters even if they don't exist.

getPopulator().populateFromStruct(
    target: this,
    trustedSetter: true,
    memento: arguments.parameters,
    excludes: ""
);

@grantcopley grantcopley added this to the 3.2 milestone Oct 25, 2023
@grantcopley grantcopley added the enhancement New feature or request label Oct 25, 2023
@grantcopley grantcopley changed the title Modify handling of missing onMount() function in wire Automatically populate data properties with passed parameters when onMount() isn't defined Oct 25, 2023
@mrigsby
Copy link
Contributor Author

mrigsby commented Oct 25, 2023

Pull request merged. Closing issue

See PR #132

@mrigsby
Copy link
Contributor Author

mrigsby commented Oct 25, 2023

Closed

@mrigsby mrigsby closed this as completed Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants