Created
August 4, 2015 19:52
-
-
Save jhusain/db542bf5e2a0b79098ab to your computer and use it in GitHub Desktop.
Redux style app
This file contains hidden or 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
// mock falcor Model | |
function Model(root, path) { | |
this.root = root; | |
this.path = path || []; | |
} | |
Model.prototype.bind = function(path) { | |
return new Model(this.root, this.path.concat(path)); | |
}; | |
var appState = { | |
entered: true, | |
genres: { | |
boundPath: ["genres"], | |
paths: [ | |
[{from: 0, to: 1}, ["name", "length"]] | |
], | |
entered: true, | |
0: { | |
boundPath: [0], | |
focused: true | |
}, | |
1: { | |
boundPath: [1] | |
} | |
} | |
}; | |
function id(x) { return x; }; | |
function mapKeys(doc, | |
recurse, acc, props){ | |
if (doc == null || typeof doc !== "object") { | |
return doc; | |
} | |
var copy = {}; | |
recurse = recurse || id; | |
props = props || {}; | |
Object. | |
keys(props). | |
forEach(function(prop) { | |
copy[prop] = props[prop]; | |
}); | |
var keys = Object.keys(doc); | |
keys.forEach(function(key) { | |
if (!(key in props)) { | |
copy[key] = recurse(doc[key], acc); | |
} | |
}); | |
return copy; | |
}; | |
var clearFocus = function clearFocus(doc, acc) { | |
if(doc.entered || doc.focused) { | |
return mapKeys( | |
doc, | |
clearFocus, | |
acc, | |
{ | |
entered: undefined, | |
focused: undefined | |
}); | |
} | |
else { | |
return doc; | |
} | |
}; | |
var setFocus = function setFocus(doc, path) { | |
if (path.length === 0) | |
return mapKeys( | |
doc, | |
null, | |
null, | |
{ focused: true }); | |
var head = path[0]; | |
if (doc[head]) { | |
var copy = mapKeys(doc, null, null, {entered: true }); | |
copy[head] = setFocus(copy[head], path.slice(1)); | |
return copy; | |
} | |
return doc; | |
}; | |
/* | |
var getPaths = function getPaths(doc, acc) { | |
var boundPath = acc.boundPath; | |
var paths = acc.paths; | |
if (doc.boundPath) { | |
boundPath = boundPath.concat(doc.boundPath); | |
} | |
if (doc.paths) { | |
paths = paths.concat(doc.paths.map(function(path) { return boundPath.concat(path))); | |
} | |
return mapKeys( | |
doc, | |
clearFocus, | |
{ | |
boundPath: boundPath, | |
paths: paths | |
}); | |
} | |
}; | |
*/ | |
var getPaths = function getPaths(doc, acc) { | |
var boundPath = acc.boundPath; | |
var paths = acc.paths; | |
if (doc.boundPath) { | |
boundPath = boundPath.concat(doc.boundPath); | |
} | |
if (doc.paths) { | |
paths = paths.concat(doc.paths.map(function(path) { return boundPath.concat(path))); | |
} | |
return mapKeys( | |
doc, | |
clearFocus, | |
{ | |
boundPath: boundPath, | |
paths: paths | |
}); | |
} | |
}; | |
var doc = clearFocus(appState); | |
document.write("<pre>" + JSON.stringify(setFocus(doc, ["genres", 1]), null, 4) + "</pre>"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment