I am trying to dynamically load a ton of controls based on the value of a dropdown and I do not want to load them in via PHP because the initial page load takes well over 15 seconds. I have a lot of controls.
So what exactly are the requirements for instantiating settings and (custom) controls via JS?
Does my custom control class need to be registered and have the content_template method fleshed out? My class needs the "type" set right? (I've done both of these things)
Can I register the settings in PHP (this is what I've done) and then instantiate the controls via JS later or do I also need to instantiate the settings in JS?
What about the JS? Do I need to repeat my markup code in the JS instantiation?
This is what I have thus far, but nothing actually shows up.. I'm clearly missing some fundamental thing but I'm not sure what:
var api = wp.customize;
var sectionID = 'site_variables_section'; // section.id.replace( '[', '-' ).replace( ']', '' )
api.controlConstructor.ColorVariable = api.Control.extend({
ready: function() {
var control = this;
wp.customize.Control.prototype.ready.call( control );
}
});
// The control
var controlID = 'site_variables_section[' + field.name.replace('$', '') + ']';
var controlParams = {
section: sectionID,
label: 'label',
settings: {
'default': controlID
},
active: true,
type: 'ColorVariable',
};
var control = new api.controlConstructor.ColorVariable( controlID, controlParams );
control.active.validate = function() {
return true;
};
api.control.add( control.id, control );
@weston-ruter got me on the right track, but I'm still a little confused by a couple things:
1- How is the value being passed into the field/saved when there is no data-customize-setting-link? Is that handled by JS content_template
2- I see that when a single setting is used for a control, the "default" key in the settings param is used, but what about when multiple settings and how do we grab the values for each setting: default
for 2, I was able to get it working with:
settings:{
variable : controlId+'[variable]',
control: controlId+'[control]',
value: controlId+'[value]',
percentage: controlId+'[percentage]'
}
I'm thinking something like this in the to_json()
function:
$this->json['percentage_value'] = $this->value('my-settings-id);