diff --git a/README.md b/README.md index 390a274..33e6728 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ requirePackages('tree-view', 'find-and-replace', 'snippets') # Do something with the required packages ``` -### registerOrUpdateElement(elementName, prototype) +### registerOrUpdateElement(elementName, options) Registers or updates a custom element whose name is `elementName`. @@ -24,13 +24,18 @@ Registers or updates a custom element whose name is `elementName`. {registerOrUpdateElement} = require 'atom-utils' class MyElement + @staticMethod: -> + console.log 'in static method' + createdCallback: -> console.log 'element created' -registerOrUpdateElement('my-element', MyElement.prototype) +MyElement = registerOrUpdateElement('my-element', class: MyElement) ``` -The update is performed by copying the properties from the passed-in prototype in the registered element prototype. As a node's callback methods can't be overriden once the element have been registered, a generic version is created that will invoke the concrete callback when called, that way even the node's callback methods can be updated. +The update is performed by copying the properties from the passed-in class and its prototype in the registered element class. As a node's callback methods can't be override once the element have been registered, a generic version is created that will invoke the concrete callback when called, that way even the node's callback methods can be updated. + +Only the class' prototype can be passed using the `prototype` option instead of `class`. ### Ancestors (previously AncestorsMethods) diff --git a/spec/mixins/ancestors-spec.coffee b/spec/mixins/ancestors-spec.coffee index 1feca8c..5edcd6b 100644 --- a/spec/mixins/ancestors-spec.coffee +++ b/spec/mixins/ancestors-spec.coffee @@ -6,7 +6,7 @@ describe 'Ancestors mixin', -> class DummyElement extends HTMLElement Ancestors.includeInto(this) - DummyElement = registerOrUpdateElement 'dummy-element-ancestors', DummyElement.prototype + DummyElement = registerOrUpdateElement 'dummy-element-ancestors', prototype: DummyElement.prototype beforeEach -> jasmineContent = document.body.querySelector('#jasmine-content') diff --git a/spec/mixins/events-delegation-spec.coffee b/spec/mixins/events-delegation-spec.coffee index d10edfb..4540136 100644 --- a/spec/mixins/events-delegation-spec.coffee +++ b/spec/mixins/events-delegation-spec.coffee @@ -18,7 +18,7 @@ describe 'EventsDelegation', -> @firstChild.appendChild(document.createElement('span')) @appendChild(document.createElement('input')) - DummyElement = registerOrUpdateElement 'dummy-element-events', DummyElement.prototype + DummyElement = registerOrUpdateElement 'dummy-element-events', prototype: DummyElement.prototype beforeEach -> jasmineContent = document.body.querySelector('#jasmine-content') diff --git a/spec/mixins/space-pen-dsl-spec.coffee b/spec/mixins/space-pen-dsl-spec.coffee index bf803d6..2183576 100644 --- a/spec/mixins/space-pen-dsl-spec.coffee +++ b/spec/mixins/space-pen-dsl-spec.coffee @@ -13,7 +13,7 @@ describe 'space-pen DSL', -> createdCallback: -> @created = true - DummyElement = registerOrUpdateElement 'dummy-element-dsl', DummyElement.prototype + DummyElement = registerOrUpdateElement 'dummy-element-dsl', prototype: DummyElement.prototype beforeEach -> element = new DummyElement @@ -46,7 +46,7 @@ describe 'space-pen DSL', -> createdCallback: -> @created = true - ShadowDummyElement = registerOrUpdateElement 'shadow-dummy-element-dsl', ShadowDummyElement.prototype + ShadowDummyElement = registerOrUpdateElement 'shadow-dummy-element-dsl', prototype: ShadowDummyElement.prototype beforeEach -> element = new ShadowDummyElement diff --git a/spec/register-or-update-element-spec.coffee b/spec/register-or-update-element-spec.coffee index a8cc377..f844d1c 100644 --- a/spec/register-or-update-element-spec.coffee +++ b/spec/register-or-update-element-spec.coffee @@ -6,7 +6,7 @@ describe 'registerOrUpdateElement', -> createdCallback: -> @name = 'dummy' - registerOrUpdateElement('dummy-element', Dummy.prototype) + registerOrUpdateElement('dummy-element', prototype: Dummy.prototype) dummy = document.createElement('dummy-element') @@ -23,7 +23,7 @@ describe 'registerOrUpdateElement', -> it 'registers a custom element when the class was created by babel', -> BabelDummy = require './fixtures/babel-dummy' - registerOrUpdateElement('babel-dummy-element', BabelDummy.prototype) + registerOrUpdateElement('babel-dummy-element', prototype: BabelDummy.prototype) dummy = document.createElement('babel-dummy-element') @@ -44,7 +44,7 @@ describe 'registerOrUpdateElement', -> update: -> @name = 'updated dummy2' - Dummy = registerOrUpdateElement('dummy-element-2', Dummy.prototype) + Dummy = registerOrUpdateElement('dummy-element-2', prototype: Dummy.prototype) dummy = document.createElement('dummy-element-2') expect(dummy.name).toEqual('dummy') @@ -52,7 +52,7 @@ describe 'registerOrUpdateElement', -> dummy.update() expect(dummy.name).toEqual('updated dummy') - Dummy = registerOrUpdateElement('dummy-element-2', Dummy2.prototype) + Dummy = registerOrUpdateElement('dummy-element-2', prototype: Dummy2.prototype) dummy = document.createElement('dummy-element-2') expect(dummy.name).toEqual('dummy2') diff --git a/src/register-or-update-element.coffee b/src/register-or-update-element.coffee index ce5a4fe..588c6b1 100644 --- a/src/register-or-update-element.coffee +++ b/src/register-or-update-element.coffee @@ -1,3 +1,4 @@ +{deprecate} = require 'grim' if window.__CUSTOM_HTML_ELEMENTS_CLASSES__? window.__ATOM_UTILS_CUSTOM_ELEMENT_CLASSES__ = window.__CUSTOM_HTML_ELEMENTS_CLASSES__ @@ -42,7 +43,11 @@ module.exports = (nodeName, options) -> if klass? proto = klass.prototype else - proto = options + proto = options.prototype ? options + + if proto is options + deprecate('Using the prototype as the second argument is deprecated, use the prototype option instead') + if __ATOM_UTILS_CUSTOM_ELEMENT_CLASSES__[nodeName] elementClass = __ATOM_UTILS_CUSTOM_ELEMENT_CLASSES__[nodeName]