Skip to content
Browse files

backtastic is born

  • Loading branch information...
0 parents commit 45d1fffc9e80503f102751fd71f571446675e45c Chris Nelson committed May 25, 2012
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in backbone_subviews.gemspec
+gemspec
1 Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
28 backtastic.gemspec
@@ -0,0 +1,28 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "backtastic/version"
+
+Gem::Specification.new do |s|
+ s.name = "backtastic"
+ s.version = Backtastic::VERSION
+ s.authors = ["Chris Nelson"]
+ s.email = ["superchrisnelson@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{Formtastic meets backbone.. ish.}
+ s.description = %q{Create backbone twitter bootstrap form views easily using helpers. Handle and display
+ validation failures from rails.
+ }
+
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ s.add_runtime_dependency "haml_coffee_assets"
+ s.add_runtime_dependency "rails-backbone"
+ s.add_runtime_dependency "inflection-js-rails"
+ s.add_runtime_dependency "twitter-bootstrap-rails"
+end
15 example/.gitignore
@@ -0,0 +1,15 @@
+# See http://help.github.com/ignore-files/ for more about ignoring files.
+#
+# If you find yourself ignoring temporary files generated by your text editor
+# or operating system, you probably want to add a global ignore instead:
+# git config --global core.excludesfile ~/.gitignore_global
+
+# Ignore bundler config
+/.bundle
+
+# Ignore the default SQLite database.
+/db/*.sqlite3
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
41 example/Gemfile
@@ -0,0 +1,41 @@
+source 'https://rubygems.org'
+
+gem 'rails', '3.2.2'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3'
+
+
+# Gems used only for assets and not required
+# in production environments by default.
+group :assets do
+ gem 'sass-rails', '~> 3.2.3'
+ gem 'coffee-rails', '~> 3.2.1'
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
+ # gem 'therubyracer'
+
+ gem "haml_coffee_assets"
+ gem "jasmine-ajax"
+ gem "backtastic", :path => ".."
+ gem 'uglifier', '>= 1.0.3'
+end
+
+gem 'jquery-rails'
+gem "jasminerice"
+gem "inherited_resources"
+# To use ActiveModel has_secure_password
+# gem 'bcrypt-ruby', '~> 3.0.0'
+
+# To use Jbuilder templates for JSON
+# gem 'jbuilder'
+
+# Use unicorn as the app server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'ruby-debug19', :require => 'ruby-debug'
12 lib/assets/javascripts/backtastic.coffee
@@ -0,0 +1,12 @@
+#= require hamlcoffee
+#= require inflection
+#= require jquery.serializeObject
+#= require_self
+#= require_tree .
+
+window.Backtastic = {
+ Views: {}
+}
+
+window.HAML.context = (ctx) -> ctx
+window.HAML.escape = (text) -> text
2 lib/assets/javascripts/templates/text_field_template.jst.hamlc
@@ -0,0 +1,2 @@
+%label{for: @field}= @label
+%input{name: @field, value: @model.get(@field)}
6 lib/assets/javascripts/views/backtastic_view.coffee
@@ -0,0 +1,6 @@
+class Backtastic.View extends Backbone.View
+
+ render: ->
+ @trigger("beforeRender")
+ @$el.html @template(@)
+ @trigger("rendered")
19 lib/assets/javascripts/views/form_field_view.coffee
@@ -0,0 +1,19 @@
+class Backtastic.Views.FormFieldView extends Backtastic.View
+
+ constructor: (options)->
+ super
+ @field = options.field
+ @label = options.label
+ @parentView = options.parentView
+ @parentView.on "rendered", => @afterParentRender()
+
+ afterParentRender: ->
+ @setElement(@parentView.$("[data-field=#{@field}]"))
+ @render()
+
+ displayErrors: (messages) ->
+ @$el.addClass "error"
+ @$el.append "<span class='help-inline'>#{message}</span>" for message in messages
+
+ clearErrors: ->
+ @$el.removeClass "error"
33 lib/assets/javascripts/views/form_view.coffee
@@ -0,0 +1,33 @@
+class Backtastic.Views.FormView extends Backtastic.View
+
+ constructor: ->
+ super
+ @fieldViews = {}
+
+ clearErrors: ->
+ fieldView.clearErrors() for field, fieldView of @fieldViews
+
+ displayErrors: (response)->
+ @$("input[type='submit']").removeAttr("disabled")
+ errors = JSON.parse(response.responseText)
+ errors = errors.errors if errors.errors #rails does it this way
+ for field, errorMessages of errors
+ @fieldViews[field]?.displayErrors(errorMessages)
+
+ textField: (options) ->
+ @fieldViews[options.field] = new Backtastic.Views.TextFieldView
+ parentView: @
+ field: options.field
+ label: options.label
+ model: @model
+ "<div data-field='#{options.field}'></div>"
+
+ save: (event)->
+ @$("input[type='submit']").attr("disabled", "disabled")
+ @clearErrors()
+ event.preventDefault()
+ @model.on "error", (model, response) => @displayErrors(response)
+ @model.save @$("form").serializeObject()
+
+ afterSave: ->
+ @$el.modal("hide")
4 lib/assets/javascripts/views/text_field_view.coffee
@@ -0,0 +1,4 @@
+class Backtastic.Views.TextFieldView extends Backtastic.Views.FormFieldView
+
+ template: JST["templates/text_field_template"]
+
9 lib/backtastic.rb
@@ -0,0 +1,9 @@
+require "backtastic/version"
+require 'rails-backbone'
+require "inflection-js-rails"
+require "twitter-bootstrap-rails"
+
+module Backtastic
+ class BacktasticEngine < Rails::Engine
+ end
+end
3 lib/backtastic/version.rb
@@ -0,0 +1,3 @@
+module Backtastic
+ VERSION = "0.0.1"
+end
19 vendor/assets/javascripts/jquery.serializeObject.js.coffee
@@ -0,0 +1,19 @@
+jQuery.fn.serializeObject = ->
+ arrayData = @serializeArray()
+ objectData = {}
+
+ jQuery.each arrayData, ->
+ if @value?
+ value = @value
+ else
+ value = ''
+
+ if objectData[@name]?
+ unless objectData[@name].push
+ objectData[@name] = [objectData[@name]]
+
+ objectData[@name].push value
+ else
+ objectData[@name] = value
+
+ return objectData

0 comments on commit 45d1fff

Please sign in to comment.
Something went wrong with that request. Please try again.