8000 Handling forms within live. by pharmac1st · Pull Request #6 · socketry/live · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Handling forms within live. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/live.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# THE SOFTWARE.

require_relative "live/version"

require_relative "live/form"
require_relative 'live/page'
require_relative 'live/view'

39 changes: 39 additions & 0 deletions lib/live/form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require_relative 'view'
require 'trenni/builder'
require 'json'

module Live
# Represents a Form within the view layer
# Differs from Live::View because the server sends both the
# details and a serialized form from the view layer.
class Form < Live::View

# returns [String] javascript code to execute when the form is submitted.
def forward_submit
"live.handleForm(this.id, event)"
end

# Render the element
def to_html()

Trenni::Builder.fragment do |builder|
builder.tag :form, method: "post", id: @id, class: 'live', onsubmit:forward_submit, data: @data do
render(builder)
end
end
end

# Handles an incoming event.
def handle(event)
self.submit(event[:details][1])
end

# Processes incoming form data, which can be used through @data[:form]
def submit(form_data)
@data[:form] = JSON.parse(form_data).to_h
end

end
end
0