Code Monkey home page Code Monkey logo

reform's Introduction

(Disclaimer: This library is under development, which means it will change a lot. If you decide to use it be cautious, and be patient)

#ReForm.js

This is a small library to work with forms on javascript. It uses (and aims to be used with) backbone.js.

##Usage:

see the usage bellow:


FormView = ReForm.Form.extend
    fields: [
        {name: 'title', widget: ReForm.commonWidgets.TextWidget, label: 'Todo:'}
        {name: 'desc', widget: ReForm.commonWidgets.TextAreaWidget, label: 'Description:'}
    ]
    events:
        'success': 'onSuccess'
        'validation': 'onValidation'

    onSuccess: (data) ->
        console.dir data

    onValidation: ->
        # do some client-side validation ()
        if has_error
          @errors {key: 'error description'}
          return false
        else
          return true

$ () ->
    form = new FormView
        formId: 'some_id'
        model: new MyModel()

    $('#my-form-wrapper').html myForm.render().el


on the constructor it accepts a model (Backbone.Model), if the model is prepoluated, the form renders with the model data already loaded.

##Methods:

*events:

submit, success, error

*form.save()

saves the data (uses model.save), This method is automatically called when you raise a 'submit' event. It raises a 'success' or 'error' event. To trigger the sucess the model.save should return a Http 200. To trigger a 'error' should return another Http status code (usually 400 Bad Request, for validation)

*form.set({title: 'some title', desc:'nonononoono'})

*form.get()

*form.get('title')

*form.errors()

*form.errors({title: 'Validations Msg'})

*form.cleanErrors()

*form.fields

the form fields, after render, have a 'instance' attribute so you play with the widget directly

##Widgets

AnnoyingWidget = ReForm.Widget.extend
    template: """
    <input class="annoying" type="text" name="<%=name%>" id=id_"<%=name%>" value="<%=value%>">
    """
    behavior: ()->
        @$el.find('.annoying').click (evt) =>
            alert 'heey, stop it!!!'
            $(evt.target).blur()

You could have provided a template from the DOM example:

On my HTMl:
<script type="text/template" id="my-widget">
    ... my widget html here!
</script>
AnnoyingWidget = ReForm.Widget.extend
    template: $('#my-widget').html()
    behavior: ()->
       .... my widget custom js here (optional)

you can (and several times, must) also provide a .set(value) and .get() function, that know how to set and get values for this widget. The default is getting from the 'input' field the .val() attribute. If your widget is more elaborate please provide these methods

###Copyright (copyleft): created by Anderson Pierre Cardoso(2012) and licensed under the terms of the MIT license.

reform's People

Contributors

andersoncardoso avatar luizarmesto avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

it3s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.