Skip to content

Instantly share code, notes, and snippets.

@michaelcarter
Created November 26, 2013 10:14
Show Gist options
  • Save michaelcarter/7656115 to your computer and use it in GitHub Desktop.
Save michaelcarter/7656115 to your computer and use it in GitHub Desktop.
Gives you dotted getters for Backbone. E.g. `@model.get("foo.bar")` vs `@model.get("foo").bar`
define (require, exports, module) ->
DottedGetters =
get: (string) ->
if !!string.match(/\./)
console.log string
@_attrFromDottedString(string)
else
@attributes[string]
_attrFromDottedString: (string) ->
keys = string.split(".")
startObject = @get(keys.shift())
@_attrOfObj(startObject, keys)
_attrOfObj: (object, keys) ->
pointer = object
for key in keys
if pointer
pointer = pointer[key]
pointer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment