Last active
August 29, 2015 14:22
-
-
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).
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
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