Skip to content

Instantly share code, notes, and snippets.

@mattzeunert
Last active February 19, 2016 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattzeunert/a9a5aebc736f9f30e53f to your computer and use it in GitHub Desktop.
Save mattzeunert/a9a5aebc736f9f30e53f to your computer and use it in GitHub Desktop.
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