[英]How to avoid code duplication in observer mutation
There is the following structure (the example itself is non-working, it is here to understand the structure): sandbox有如下结构(例子本身是不工作的,到这里来了解结构): sandbox
And in this structure some actions are repeated.在这个结构中,一些动作被重复。 How to avoid JS code duplication?
如何避免 JS 代码重复? For example in self-invoking functions.
例如在自调用函数中。
// ObserverAddRule function ObserverAddRule() { const target1 = document.querySelector('#form_edit_rule.sc') const target2 = document.querySelector('#form_edit_rule.sc1') const config = { childList: true } const callback = function (mutationsList, observer) { for (let mutation of mutationsList) { if (mutation.type === 'childList') { (function(){ let scFrstItem = Array.from(document.querySelectorAll('.sc.acrList.frstItem.mySelect')).slice(-1) let scScndItem = Array.from(document.querySelectorAll('.sc.acrList.scndItem.scnd-title.mySelect')).slice(-1) let scElements = scFrstItem.concat(scScndItem) initChoices(false, '', false, false, scElements) delegateEventChoices() // <here you need to pass.sc> })(); (function(){ let sc1FrstItem = Array.from(document.querySelectorAll('.sc1.acrList.frstItem.mySelect')).slice(-1) let sc1ScndItem = Array.from(document.querySelectorAll('.sc1.acrList.scndItem.scnd-title.mySelect')).slice(-1) let sc1Elements = sc1FrstItem.concat(sc1ScndItem) initChoices(false, '', false, false, sc1Elements) delegateEventChoices() // <here you need to pass.sc1> })(); } } } const observer = new MutationObserver(callback) observer.observe(target1, config) observer.observe(target2, config) }
<form action="" id="form_edit_rule" method="post"> <div class="sc"> <div class="acrList"> <div class="frstItem"> <p class="mySelect">ТЕКСТ</p> </div> <div class="scndItem scnd-title"> <p class="mySelect">ТЕКСТ</p> </div> </div> </div> <br><br> <div class="sc1"> <div class="acrList"> <div class="frstItem"> <p class="mySelect">ТЕКСТ</p> </div> <div class="scndItem scnd-title"> <p class="mySelect">ТЕКСТ</p> </div> </div> </div> </form>
I really do not want to hang the mutation of the observer on their common parent - to redo a lot.我真的不想把观察者的突变挂在他们共同的父母身上——重做很多。 But if this is the only method, then write about it
但如果这是唯一的方法,那就写一下吧
Only way to avoid duplication is by using parameters.避免重复的唯一方法是使用参数。
Self-invoked function is intended to run on-spot, only once, it's not intended to repeat.自调用 function 旨在现场运行,仅一次,不再重复。
So, if you have repeating functionality, then separate function(s) and/or use parameters for changeable data.所以,如果你有重复的功能,那么分离函数和/或使用可变数据的参数。
Two possible solutions:两种可能的解决方案:
https://jsfiddle.net/uxcva6wy/1/ https://jsfiddle.net/uxcva6wy/1/
const callback = function (mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
(function(namespaces){
for(let namespace in namespaces) {
let scFrstItem = Array.from(document.querySelectorAll(namespace + ' .acrList .frstItem .mySelect')).slice(-1)
let scScndItem = Array.from(document.querySelectorAll(namespace + ' .acrList .scndItem.scnd-title .mySelect')).slice(-1)
let scElements = scFrstItem.concat(scScndItem)
initChoices(false, '', false, false, scElements)
delegateEventChoices() // <here you need to pass .sc>
}
})(['.sc', '.sc1']);
}
}
}
https://jsfiddle.net/uxcva6wy/2/ https://jsfiddle.net/uxcva6wy/2/
const doSomething = function(namespace){
let scFrstItem = Array.from(document.querySelectorAll(namespace + ' .acrList .frstItem .mySelect')).slice(-1)
let scScndItem = Array.from(document.querySelectorAll(namespace + ' .acrList .scndItem.scnd-title .mySelect')).slice(-1)
let scElements = scFrstItem.concat(scScndItem)
initChoices(false, '', false, false, scElements)
delegateEventChoices() // <here you need to pass .sc>
};
const callback = function (mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
doSomething('.sc');
doSomething('.sc1');
}
}
}
If you need more specific answer, please post more specific snippet.如果您需要更具体的答案,请发布更具体的片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.