canjs / can-stache-element

Create custom elements with can-stache and can-define-class
https://canjs.com/doc/can-stache-element.html
MIT License
3 stars 1 forks source link

Finalize lifecycle methods #23

Closed phillipskevin closed 5 years ago

phillipskevin commented 5 years ago

Lifecycle methods

Called by connectedCallback; implemented by CanJS mixins; can be called by user (for testing); if implemented by user, must call super.<method>(); each method calls all the previous methods if they have not been called (passing props); Once called, each method is a no-op if called again.

Called by disconnectedCallback; implemented by CanJS mixins; can be called by user (for testing); if implemented by user, must call super.<method>(); Once called, method is a no-op if called again.

Lifecycle hooks

Called by connect; can be implemented by user without having to call super.<method>().

Called by disconnect; can be implemented by user without having to call super.<method>().

Example

class MyElement extends StacheDefineElement {}
const myElement = new MyElement();

// can call lifecycle methods for testing
myElement.initialize(); // "ViewModel" ready to be tested
myElement.render(); // view ready to be tested

// can implement lifecycle methods
class MyElement2 extends StacheDefineElement {
    connected() {
        let timeoutId = setTimeout(() => {}, 1000);
        return () => { clearTimeout(timeoutId); };
    }
}

const myElement2 = new MyElement();
myElement2.connect(); // can test the view