LeviLamina
LeviLamina copied to clipboard
CustomKVForm API
@PeterXiong720 大佬提出的建议
(CustomKVForm 为暂定名, CustomObjectForm CustomKeyForm CustomFormWithKey 也在考虑中)
从 https://www.minebbs.com/threads/lxl-forms-helper-lxl.8208/ 中获得的灵感。 如原帖所述,原来的 CustomForm 扩展性不足,组件与其添加的原始顺序强耦合。 如果要在原先的组件之间添加组件,极为困难,需要修改所有数组索引。
let form = mc.newCustomForm();
form = form.addInput("in 1");
form = form.addInput("in2");
pl.sendForm(form, (pl, data)=>{
//data[0]
//data[1]
});
参考原帖,我提议添加一个名为 CustomKVForm
的类,返回Object而不是Array作为data,去除对于组件顺序的依赖。
let form = mc.newCustomKVForm();
form = form.addInput("in1", "in 1");
form = form.addInput("in2", "in2");
pl.sendForm(form, (pl, data)=>{
//data["in1"]
//data["in2"]
});
其区别是,所有的 addXxx
方法添加一个key作为键(其中key的类型为 String
)。
例如,原来的
fm.addInput(title[, placeholder, default])
变为
fm.addInput(key, title[, placeholder, default])
对于
pl.sendForm(fm,callback)
重载sendForm。
在 fm 是 CustomKVForm 的实例时,以 (Player, Object) 而不是 (Player, Array) 的接口回调。
而Object的键就使用当初指定的那些字符串作为键。