Skip to content

Commit

Permalink
Add convenient constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 24, 2024
1 parent 282f6e3 commit b47e92a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/live/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ def self.unique_id
SecureRandom.uuid
end

# Mount an element within a parent element.
def self.mount(parent, id, data = {})
full_id = parent.id + ":" + id

self.new(full_id, data)
end

# Create a new element.
def self.[](id = self.unique_id, **data)
self.new(id, data)
end

# Create a new element.
#
# @parameter id [String] The unique identifier within the page.
# @parameter data [Hash] The data associated with the element, typically stored as `data-` attributes.
def initialize(id = Element.unique_id, data = {})
def initialize(id = self.class.unique_id, data = {})
data[:class] ||= self.class.name

@id = id
@data = data
@data[:class] ||= self.class.name
@page = nil
end

Expand Down

0 comments on commit b47e92a

Please sign in to comment.