Skip to content

Instantly share code, notes, and snippets.

@aaronj1335
Created November 15, 2013 14:37
Show Gist options
  • Select an option

  • Save aaronj1335/7485262 to your computer and use it in GitHub Desktop.

Select an option

Save aaronj1335/7485262 to your computer and use it in GitHub Desktop.
define([
'msgme/underscore'
'msgme/ko'
], function (_, ko) {
var kounwrap = ko.utils.unwrapObservable;
return function (path) {
path = (path || '').split('.');
return _.reduce(path.slice(1), function (item, prop) {
return item && kounwrap(item[prop]);
}, kounwrap(path[0]));
}
});
define([
'msgme/ko',
'msgme/util/unwrap'
], function (unwrap) {
var mapping = {
defauls: {
data: null
}
};
var vm = ko.mapping.fromJS({}, mappping);
unwrap(vm, 'data.foo.bar'); // returns undefined, no exception
vm.data({foo: {bar: 123}});
unwrap(vm, 'data.foo.bar'); // returns 123
vm.data({foo: {bar: ko.observable(123)}});
unwrap(vm, 'data.foo.bar'); // returns 123
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment