Ruby on rails Backbone.js的表单生成器插件?
Backbone.js有没有像“form_for”对Rails那样的插件?e、 例如,我提供了一个模型,它提供了一个用于构建表单的DSL?我认为它是一个具有不同解决方案的不同野兽。我编写了一个backbone.js扩展,用于将表单元素绑定到backbone.Model字段。请原谅咖啡脚本,但我通常会做以下事情Ruby on rails Backbone.js的表单生成器插件?,ruby-on-rails,forms,backbone.js,Ruby On Rails,Forms,Backbone.js,Backbone.js有没有像“form_for”对Rails那样的插件?e、 例如,我提供了一个模型,它提供了一个用于构建表单的DSL?我认为它是一个具有不同解决方案的不同野兽。我编写了一个backbone.js扩展,用于将表单元素绑定到backbone.Model字段。请原谅咖啡脚本,但我通常会做以下事情 class FooView extends MyView tag: "div" modelBindings: "change form input.address"
class FooView extends MyView
tag: "div"
modelBindings:
"change form input.address" : "address"
"change form input.name" : "name"
"change form input.email" : "email"
render: ->
$(@el).html """
<form>
<input class="address"/>
<input class="name"/>
<input class="email"/>
</form>
"""
super
@
# Instantiate the view
view = new FooView
model: new Backbone.Model
$("body").html(view.el)
我在这里写了一篇关于这个的小博客文章
我不熟悉Rails如何创建表单,但我创建了一个主干表单库,可以满足您的需求。您编写了一个简单的表单架构,它将为您生成表单:
我编写了jquery插件,允许您将json对象绑定到表单。我知道这不是你想要的。我甚至在考虑扩展它,从对象定义开始构建html,但我认为创建足够灵活的插件可能有点困难。无论如何,代码中有一些解释。它仍然是alpha版本,但它可以工作。最后对我来说还可以:D 您还可以在屏幕上抓取代码
class MyView extends Backbone.View
render: ->
if @model != null
# Iterate through all bindings
for selector, field of @modelBindings
do (selector, field) =>
console.log "binding #{selector} to #{field}"
# When the model changes update the form
# elements
@model.bind "change:#{field}", (model, val)=>
console.log "model[#{field}] => #{selector}"
@$(selector).val(val)
# When the form changes update the model
[event, selector...] = selector.split(" ")
selector = selector.join(" ")
@$(selector).bind event, (ev)=>
console.log "form[#{selector}] => #{field}"
data = {}
data[field] = @$(ev.target).val()
@model.set data
# Set the initial value of the form
# elements
@$(selector).val(@model.get(field))
super
@