Last active
February 19, 2016 17:43
-
-
Save mattzeunert/a9a5aebc736f9f30e53f to your computer and use it in GitHub Desktop.
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
function getLevelIndentation(level){ | |
return level * 20 + "px"; | |
} | |
window.devtoolsFormatters = [{ | |
header: function(obj, config){ | |
if (config && config.backboneModelFormatter){ | |
return ["div", {}, config.key] | |
} | |
if (!(obj instanceof Backbone.Model)){ | |
return null; | |
} | |
return ["div", {}, "Backbone Model with properties " + Object.keys(obj.attributes).join(", ")] | |
}, | |
hasBody: function(){ | |
return true; | |
}, | |
body: function(obj, config){ | |
var level = config !== undefined ? config.level : 0; | |
if (obj instanceof Backbone.Model) { | |
obj = obj.attributes; | |
} | |
var elements = Object.keys(obj).map(function(key){ | |
var child; | |
var childObj = obj[key]; | |
if (typeof childObj === "object"){ | |
child = ["object", { | |
object: childObj, | |
config: { | |
key: key, | |
backboneModelFormatter: true, | |
level: level + 1 | |
} | |
}]; | |
} else { | |
child = key + ": " + childObj.toString(); | |
} | |
return ["div", {style: "margin-left: " + getLevelIndentation(level)}, child]; | |
}) | |
return ["div", {}].concat(elements); | |
} | |
}] | |
var model = new Backbone.Model({ | |
title: "Buy milk", | |
author: { | |
firstName: "John", | |
lastName: "Smith", | |
address: { | |
line1: "55 Greenwood Street", | |
city: "London" | |
} | |
} | |
}) | |
console.log(model); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment