Skip to content

Commit 6e51e41

Browse files
committed
Add unit test and documentation for __getPath method.
1 parent 90aa1f4 commit 6e51e41

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# :eyes: Observable Slim
22

3-
[![Build Status](https://travis-ci.org/ElliotNB/observable-slim.svg?branch=master)](https://travis-ci.org/ElliotNB/observable-slim) [![Coverage Status](https://coveralls.io/repos/github/ElliotNB/observable-slim/badge.svg)](https://coveralls.io/github/ElliotNB/observable-slim)
3+
[![Build Status](https://travis-ci.org/ElliotNB/observable-slim.svg?branch=master)](https://travis-ci.org/ElliotNB/observable-slim) [![Coverage Status](https://coveralls.io/repos/github/ElliotNB/observable-slim/badge.svg)](https://coveralls.io/github/ElliotNB/observable-slim) [![Monthly Downloads](https://img.shields.io/npm/dm/observable-slim.svg)](https://www.npmjs.com/package/observable-slim)
44

55
https://github.com/elliotnb/observable-slim
66

@@ -25,7 +25,7 @@ down to roughly 3000 characters.
2525
Also available via NPM:
2626

2727
```
28-
$ npm install observable-slim
28+
$ npm install observable-slim --save
2929
```
3030

3131
## Usage
@@ -210,6 +210,21 @@ function traverseUp(childObj) {
210210
traverseUp(proxy.hello.foo);
211211
```
212212

213+
**Note:** This functionality is not supported by the ES5 Proxy polyfill.
214+
215+
### Retrieve the path of an object relative to the top-level observer
216+
217+
ObservablesSlim also allows you to retrieve the full path of an object relative to the top-level observed object:
218+
219+
```javascript
220+
var data = {"foo":"bar","arr":[{"test":{}}],"test":{"deeper":{}}};
221+
var p = ObservableSlim.create(data, false, function(changes) {});
222+
223+
console.log(p.test.deeper.__getPath); // logs "test.deeper"
224+
225+
```
226+
227+
**Note:** This functionality is not supported by the ES5 Proxy polyfill.
213228

214229
## Requirements
215230

observable-slim.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ var ObservableSlim = (function() {
157157
parentPath.splice(-(i+1),(i+1));
158158
return _getProperty(observable.parentProxy, parentPath.join("."));
159159
}
160+
// return the full path of the current object relative to the parent observable
160161
} else if (property === "__getPath") {
162+
// strip off the 12 characters for ".__getParent"
161163
var parentPath = _getPath(target, "__getParent");
162164
return parentPath.slice(0, -12);
163165
}

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,15 @@ function suite(proxy) {
766766

767767
});
768768

769+
it('41. Verify __getPath returns correct path (not supported with ES5 polyfill).', () => {
770+
if (global.Proxy === global.NativeProxy) {
771+
var data = {"foo":"bar","arr":[{"test":{}}],"test":{"deeper":{}}};
772+
var p = ObservableSlim.create(data, false, function(changes) {});
773+
774+
expect(p.test.deeper.__getPath).to.equal("test.deeper");
775+
expect(p.arr[0].test.__getPath).to.equal("arr.0.test");
776+
}
777+
778+
});
779+
769780
};

0 commit comments

Comments
 (0)