Created
November 26, 2013 10:14
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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