Skip to content

Instantly share code, notes, and snippets.

@tjstebbing
Created January 5, 2015 12:22
Show Gist options
  • Save tjstebbing/e1b14e5c69ca4ca7799d to your computer and use it in GitHub Desktop.
Save tjstebbing/e1b14e5c69ca4ca7799d to your computer and use it in GitHub Desktop.
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