Form builder plugin for Backbone.js?

Are there any plugins for Backbone.js that do what "form_for" does for Rails? for example, do I provide a model and provide DSL for creating a form?

+5
source share
3 answers

It is not known how Rails creates forms, but I created a Backbone form library that can do what you are looking for. You write a simple form outline, and it will generate forms for you:

https://github.com/powmedia/backbone-forms

+4
source

, . backbone.js Backbone.Model. coffeescript, .

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) 

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

    @

.

http://xtargets.com/2011/06/11/binding-model-attributes-to-form-elements-with-backbone-js/

+1

jquery plugin, json . , , . , , html , , , . - . -, . , : D

jquery plugin

0

All Articles