Skip to content

Instantly share code, notes, and snippets.

@jamescook
Created November 23, 2010 05:12
Show Gist options
  • Save jamescook/711293 to your computer and use it in GitHub Desktop.
Save jamescook/711293 to your computer and use it in GitHub Desktop.
class ActiveRecordBase extends Backbone.Model
url: "/"
fields: []
toJSON: () ->
@attributes.authenticity_token = @token()
return _.clone(@attributes)
fill: (selector) ->
form = jQuery(selector)
ns = @attributes[@namespace]
data = ns || @attributes
keys = @fields
for _el in form.find("select,input,textarea")
for key in keys
_el = jQuery(_el)
if _el.attr("name") == @namespace + "[" + key + "]"
_el.val(data[key])
true
from: (selector) ->
form = jQuery(selector)
if @namespace
ns = @attributes[@namespace]
if typeof ns is "undefined"
ns = {}
for key in @fields
val = form.find("*[name='" + @namespace + "["+ key +"]" + "']").val()
(ns[key] = val )
@attributes[@namespace] = ns
true
else
@attributes[key] = form.find("*[name='" + key + "']").val() for key in @fields
true
token: ->
return jQuery('body').find("input[name='authenticity_token']:first").val()
class Ticket extends ActiveRecordBase
url: "/tickets"
fields: ["name", "project_id", "description"]
namespace: "ticket"
class WorkUnit extends ActiveRecordBase
url: "/work_units"
fields: ["name", "ticket_id", "description"]
namespace: "work_unit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment