{"action":"created","issue":{"url":"https://api.github.com/repos/aurelia/binding/issues/266","labels_url":"https://api.github.com/repos/aurelia/binding/issues/266/labels{/name}","comments_url":"https://api.github.com/repos/aurelia/binding/issues/266/comments","events_url":"https://api.github.com/repos/aurelia/binding/issues/266/events","html_url":"https://github.com/aurelia/binding/issues/266","id":123160247,"number":266,"title":"Suggestion: API for traversing binding context","user":{"login":"genadis","id":6747147,"avatar_url":"https://avatars.githubusercontent.com/u/6747147?v=3","gravatar_id":"","url":"https://api.github.com/users/genadis","html_url":"https://github.com/genadis","followers_url":"https://api.github.com/users/genadis/followers","following_url":"https://api.github.com/users/genadis/following{/other_user}","gists_url":"https://api.github.com/users/genadis/gists{/gist_id}","starred_url":"https://api.github.com/users/genadis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/genadis/subscriptions","organizations_url":"https://api.github.com/users/genadis/orgs","repos_url":"https://api.github.com/users/genadis/repos","events_url":"https://api.github.com/users/genadis/events{/privacy}","received_events_url":"https://api.github.com/users/genadis/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aurelia/binding/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-12-20T13:40:08Z","updated_at":"2015-12-20T22:51:56Z","closed_at":null,"body":"Hi,\r\n\r\nI believe that a set of APIs for traversing the binding context, something like jQuery style for traversing the DOM could be a very useful feature for Aurelia.\r\n\r\nFor example API for finding specific binding context parent based on Class type? Something like $.closest() of jQuery.\r\n\r\nI've implemented something basic that i find useful:\r\n```\r\nexport function getClosestContext(overrideContext: Object, contextClass: Function) {\r\n  let bindingContext = null;\r\n  let context = overrideContext;\r\n  do {\r\n    if (context.bindingContext instanceof contextClass) {\r\n      bindingContext = context.bindingContext;\r\n      break;\r\n    }\r\n    context = context.parentOverrideContext;\r\n  } while (context);\r\n\r\n  return bindingContext;\r\n}\r\n```\r\n\r\nWhich i use in `bind` context of my VM:\r\n```\r\n// Some child custom element\r\n  bind(bindingContext: Object, overrideContext: Object) {\r\n    this.usefulParent = getClosestContext(overrideContext, SomeParentClass);\r\n  }\r\n```\r\n\r\nI find it more useful instead of passing the context through each custom element of the DOM.\r\nIf I decide to change the structure of the DOM, I don't need to worry about passing the parent context binding.\r\n"},"comment":{"url":"https://api.github.com/repos/aurelia/binding/issues/comments/166160563","html_url":"https://github.com/aurelia/binding/issues/266#issuecomment-166160563","issue_url":"https://api.github.com/repos/aurelia/binding/issues/266","id":166160563,"user":{"login":"jdanyow","id":1306747,"avatar_url":"https://avatars.githubusercontent.com/u/1306747?v=3","gravatar_id":"","url":"https://api.github.com/users/jdanyow","html_url":"https://github.com/jdanyow","followers_url":"https://api.github.com/users/jdanyow/followers","following_url":"https://api.github.com/users/jdanyow/following{/other_user}","gists_url":"https://api.github.com/users/jdanyow/gists{/gist_id}","starred_url":"https://api.github.com/users/jdanyow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdanyow/subscriptions","organizations_url":"https://api.github.com/users/jdanyow/orgs","repos_url":"https://api.github.com/users/jdanyow/repos","events_url":"https://api.github.com/users/jdanyow/events{/privacy}","received_events_url":"https://api.github.com/users/jdanyow/received_events","type":"User","site_admin":false},"created_at":"2015-12-20T22:51:56Z","updated_at":"2015-12-20T22:51:56Z","body":"@genadis sure- you can register anything with any key.  The key doesn't need to be a constructor function/class. It could be a string, a number, an object instance, anything...  For example:\r\n\r\n```js\r\nlet a = new Something();\r\nlet b = new Something();\r\ncontainer.registerInstance('some key', a);\r\ncontainer.registerInstance('another key', b);\r\n```\r\nthen:\r\n```js\r\n@inject('some key', 'another key')\r\nexport class Foo {\r\n  constructor(a, b) {\r\n    ...\r\n  }\r\n  ...\r\n}\r\n```"}}