ورشة أسرار Vue: يلا نعمل VDOM مثل الموجودة في اطار عمل Vuejs

مرحبا شباب :star_struck:

من زمان معملتش ورشات…

حصريا على Coretabs

هذه ورشة سريعة لعمل VDOM هتخلينا نفهم كيف بتشتغل

مشاهدة ممتعة :star_struck:

1 Like

السلام عليكم
شكرا على هذه الورشة والشرح المبسط والأكثر من رائع
أعجبتني فكرة بناء vdom صغنن كما قلت :rofl: فأردت بدوري المشاركة في هذه الورشة ولو أنني لم اضف شيئا، فقط كتفاعل طبقت الفكرة لغاية المرحلة التي وصلت اليها في هذا الفيديو لكن بطريقة مختلفة js class-based :sweat_smile: :sweat_smile: :sweat_smile:

style.css

.green {
    color: green
}

.red {
    color: red
}

.link {
    text-decoration: none;
    color: mediumorchid;
}

script.js

class H {
    
    constructor(tag, props, children) {
        this.tag = tag
        this.props = props
        this.children = children
        this.el = document.createElement(this.tag)
    }

    setAttributes() {
        if (this.props) {
            for (const key in this.props) {
                if (Object.hasOwnProperty.call(this.props, key)) {
                    const value = this.props[key]
                    this.el.setAttribute(key, value)     
                }
            }
        }
    }

    mount(container) {
        if (this.children) {
            if (typeof this.children === 'string') {
                this.el.innerHTML = this.children
            } else {
                this.children.map(child => {
                    child.mount(this.el)
                })
            }
        }
        
        this.setAttributes()
        return container.appendChild(this.el)
    }
}



// Get the main container
const container = document.querySelector('#app')

// Example 1
const vnode = new H('div', {class: 'green'}, 'Hi')

// Example 2
const vnodeWithChildren = new H('div', {class: 'green'}, [
    new H('span', {class: 'red'}, 'I am span, '),
    new H('a', {href: 'https://google.com'}, 'Take me to google'),
    
])

// Example 3
const vnodeWithChildren2 = new H('div', {class: 'green'}, [
    new H('span', {class: 'red'}, 'I am span, '),
    new H(
        'a',
        {
            href: 'https://cortabs.net',
            target: '_BLANK',
            class: 'link',
            id: 'coretabs'
        },
        'Take me to cortabs'
        ),
    new H('span', {id: 'imoj'}, ' 😀'),
])

vnode.mount(container)
vnodeWithChildren.mount(container)
vnodeWithChildren2.mount(container)
1 Like

حلاوة يا @L.Da :star_struck:

ان شاء الله هنستخدم الclass based في صناعة reactivity system صغنن :joy:

حلوة الفكرة :tada:

1 Like