Skip to content

Instantly share code, notes, and snippets.

@kevinthompson
Last active October 13, 2015 15:48
Show Gist options
  • Select an option

  • Save kevinthompson/4219217 to your computer and use it in GitHub Desktop.

Select an option

Save kevinthompson/4219217 to your computer and use it in GitHub Desktop.
Supporting Rails Nested Resources in Batman.js
class App.Model extends Batman.Model
@persist App.Storage
@encodeAttributesFor: ->
@encodeAttributesForKeys ?= []
for key in arguments
if typeof key == 'string'
@encodeAttributesForKeys.splice(index,1) if (index = @encodeAttributesForKeys.indexOf(key)) > 0
@encodeAttributesForKeys.push key
class App.Storage extends Batman.RailsStorage
@::before 'create', 'update', (env, next) ->
if env.subject.constructor.encodeAttributesForKeys?
data = env.options.data
if namespace = @recordJsonNamespace(env.subject)
obj = data[namespace]
else
obj = data
for key in env.subject.constructor.encodeAttributesForKeys
if obj[key]
obj["#{key}_attributes"] = obj[key] if obj[key].length
delete obj[key]
next()
@::after 'update', @skipIfError (env, next) ->
if env.subject and env.subject.constructor.encodeAttributesForKeys?
for key in env.subject.constructor.encodeAttributesForKeys
objects = env.subject.get(key)
if objects?.constructor.name == 'AssociationSet'
objects.forEach (object) ->
if object.get('_destroy')
objects.remove(object)
else
object.get('dirtyKeys').clear()
object.get('_dirtiedKeys').clear()
class App.Store extends App.Model
@hasMany 'products'
@encodeAttributesFor 'products'
@turizoft

Copy link
Copy Markdown

Any chance of this getting merged into BatmanJS core?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment