Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cfenzo
Last active August 29, 2015 14:22
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 cfenzo/3b514e4b801b862859bd to your computer and use it in GitHub Desktop.
Save cfenzo/3b514e4b801b862859bd to your computer and use it in GitHub Desktop.
Ractive.js: Resolve mapped keypaths from child instances to its correct keypath in the calling instance (if they originate from that instance or one of its parents).
function keypath_resolve(keypath, instance, caller){
return (function _resolve(keypath, instance, caller){
var keypathArray, mapped;
// if instance is the caller, return the current keypath
if(instance === caller) {
return keypath;
}
// split the keypath, look for the first key in viewmodel mappings
keypathArray = keypath.split('.');
mapped = instance.viewmodel.mappings[keypathArray[0]];
// if nothing is mapped for the first key, this is as far as we get.. (and we haven't reached the caller, thus returning false)
if(!mapped) {
return false;
}
// replace or first key with the mapped keypath, and make a new keypath
keypathArray[0] = mapped.keypath.str;
keypath = keypathArray.join('.');
// as long as the first key is mapped, there is a parent
return _resolve(keypath, instance.parent, caller)
})(keypath, instance, caller);
}
// example usage:
instance.on('*.bubbled',function(event){
var keypath = resolve_keypath(event.keypath, event.ractive, this);
this.set(keypath,'foo');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment