Created
January 5, 2015 12:22
-
-
Save tjstebbing/e1b14e5c69ca4ca7799d to your computer and use it in GitHub Desktop.
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
var rv = require("rivets"); | |
var fickle = require("fickle"); | |
// Attempt at making a rivets/fickle adapter that remembers keypaths, | |
// really doesn't work when you start trying to set values :/ | |
function adapt() { | |
var ctx = fickle.context(); | |
history = {}; | |
function stack(verb, obj, keypath) { | |
//Keep last accessed fickle object and full keypaths | |
if(!history[verb]) history[verb] = {obj : null, path : ''}; | |
h = history[verb]; | |
if(obj._hook) { | |
h.obj = obj; | |
h.path = keypath; | |
} else { | |
h.path += '.'+keypath; | |
} | |
return h; | |
} | |
rv.adapters['.'] = { | |
observe: function(obj, keypath, callback) { | |
var h = stack('observe', obj, keypath); | |
console.log('observe', keypath, h); | |
ctx.on(h.obj, h.path, callback); | |
}, | |
unobserve: function(obj, keypath, callback) { | |
var h = stack('unobserve', obj, keypath); | |
console.log('unobserve', keypath, h); | |
ctx.clear(h.obj, h.path, callback); | |
}, | |
get: function(obj, keypath) { | |
var h = stack('get', obj, keypath); | |
console.log('get', keypath, h); | |
return h.obj.get(h.path); | |
}, | |
set: function(obj, keypath, value) { | |
var h = stack('get', obj, keypath); | |
console.log('set', obj, keypath, h.obj, h.path); | |
return h.obj.get(h.path); | |
} | |
}; | |
} | |
adapt(); | |
var page = fickle({ | |
title : "HELLO WORLD", | |
data : { | |
firstname : "Pomke", | |
lastname : "Nohkan" | |
} | |
}); | |
rv.bind(document, page); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment