Created
November 15, 2013 14:37
-
-
Save aaronj1335/7485262 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
| 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])); | |
| } | |
| }); |
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
| 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