Skip to content

Commit

Permalink
Deprecate the prototype as second argument in registerOrUpdateElement
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Jan 28, 2016
1 parent 4c7e77a commit 74796e2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ 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`.

```coffee
{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)

Expand Down
2 changes: 1 addition & 1 deletion spec/mixins/ancestors-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion spec/mixins/events-delegation-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions spec/mixins/space-pen-dsl-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/register-or-update-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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')

Expand All @@ -44,15 +44,15 @@ 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')

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')
Expand Down
7 changes: 6 additions & 1 deletion src/register-or-update-element.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{deprecate} = require 'grim'

if window.__CUSTOM_HTML_ELEMENTS_CLASSES__?
window.__ATOM_UTILS_CUSTOM_ELEMENT_CLASSES__ = window.__CUSTOM_HTML_ELEMENTS_CLASSES__
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 74796e2

Please sign in to comment.