Title: Proposal: Support declarative binding from View events to Observables · Issue #4062 · angular/angular · GitHub
Open Graph Title: Proposal: Support declarative binding from View events to Observables · Issue #4062 · angular/angular
X Title: Proposal: Support declarative binding from View events to Observables · Issue #4062 · angular/angular
Description: Not sure this is a good idea, or even possible, but thought I'd throw it out there. In vanilla Rx, if I wanted to listen to a DOM event to do a typeahead or something I'd do something like: //get DOM element const input = document.getEle...
Open Graph Description: Not sure this is a good idea, or even possible, but thought I'd throw it out there. In vanilla Rx, if I wanted to listen to a DOM event to do a typeahead or something I'd do something like: //get D...
X Description: Not sure this is a good idea, or even possible, but thought I'd throw it out there. In vanilla Rx, if I wanted to listen to a DOM event to do a typeahead or something I'd do something like:...
Opengraph URL: https://github.com/angular/angular/issues/4062
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Support declarative binding from View events to Observables","articleBody":"Not sure this is a good idea, or even possible, but thought I'd throw it out there.\r\n\r\nIn vanilla Rx, if I wanted to listen to a DOM event to do a typeahead or something I'd do something like:\r\n\r\n``` js\r\n//get DOM element\r\nconst input = document.getElementById('myInput');\r\n//observable of click events\r\nconst inputs$ = Rx.Observable.fromEvent(input, 'keyup');\r\n//map changes to requests\r\nconst responses$ = inputs$.flatMap(ev => myHttpService.get(ev.target.value)); \r\n//subscribe to responses\r\nresponses$.subscribe(json => console.log(json));\r\n```\r\n\r\nIn current ng2, if I wanted to do the equivalent, I'd do something like:\r\n\r\n``` js\r\n@Component({\r\n selector: 'my-component'\r\n})\r\n@View({\r\n template: `\r\n \u003cdiv [ng-form-model]=\"myForm\">\r\n \u003cinput type=\"text\" ng-control=\"query\">\r\n \u003c/div>\r\n \u003cul>\r\n \u003cli *ng-for=\"#result of results | async\">{{result.text}}\u003c/li>\r\n \u003c/ul>\r\n`\r\n})\r\nclass MyComponent {\r\n constructor(public http: Http, fb: FormBuilder){\r\n //create form group\r\n this.myForm = fb.group({\r\n query: \"\"\r\n });\r\n //subscribe to value changes, map to responses, bind to view w/ pipe\r\n this.results = this.myForm.controls.query.valueChanges\r\n .toRx()\r\n .flatMap(query => this.http.get(`/foos?q=${query}`)\r\n .map(res => res.json());\r\n }\r\n}\r\n```\r\n\r\nThis all works great (or will, once Rx stuff settles).\r\n\r\nHowever, if I wanted to do the same sort of thing from an arbitrary event (unrelated to forms, like a mousedrag or button click or whatever), there's not a good way (I can see) to get an Observable from a DOM event, so you have to do something like:\r\n\r\n``` html\r\n\u003cbutton (click)=\"makeRequest($event)\">Make Request\u003c/button>\r\n```\r\n\r\n``` js\r\n//snip\r\nclass MyComponent {\r\n makeRequest($event){\r\n this.http.get($event.target.url)\r\n .map(res => res.json())\r\n .subscribe(results => this.results = results);\r\n }\r\n}\r\n```\r\n\r\nwhich isn't nearly as clean.\r\n\r\nProposed idea would be to provide a way to delegate / bind an arbitrary DOM event to a Subject/Observable, so that it could be easily subscribed to and handled reactively. \r\n\r\n~~Something like (totally made up syntax here, point being we're binding the click event to the Subject):~~\r\n\r\nEdit: see https://github.com/angular/angular/issues/4062#issuecomment-231615401 for current proposal\r\n\r\n``` html\r\n\u003cbutton [(click)]=\"clicks$\">make request\u003c/button>\r\n```\r\n\r\n``` js\r\nclass MyComponent {\r\n clicks$ = new Rx.Subject();\r\n constructor(){\r\n this.results = this.clicks$\r\n .flatMap(ev => http.get('someurl.json'))\r\n .map(res => res.json())\r\n }\r\n}\r\n```\r\n\r\nI saw https://github.com/angular/angular/pull/3992, but I believe this is for emitting events \"out\" from the component, whereas this idea is more for usage within a component.\r\n\r\nThoughts?\r\n","author":{"url":"https://github.com/robwormald","@type":"Person","name":"robwormald"},"datePublished":"2015-09-08T23:48:26Z","url":"https://github.com/angular/angular/issues/4062"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0fc0ba48-117c-0bd0-050c-9ca3ee6ef959 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B94E:27D3AD:1BCE8E2:2345724:6B0152AB |
| html-safe-nonce | d3d2ed69938bdb7749269641396169981c3b18d6bbda4f816d0a26bf104a942c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTRFOjI3RDNBRDoxQkNFOEUyOjIzNDU3MjQ6NkIwMTUyQUIiLCJ2aXNpdG9yX2lkIjoiNjE4MDY5MDU5MDUyNzczODUzOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c46b10385e78fa40c42b293b3b4f4693b0e04cc8b0a1fcf89d3579ddb45eb47e |
| hovercard-subject-tag | issue:105490360 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/4062/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9fd27defc33c3105f6d5bf59abd3c90c31d3992e92c72088499c554eb250f829/angular/angular/issues/4062 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9fd27defc33c3105f6d5bf59abd3c90c31d3992e92c72088499c554eb250f829/angular/angular/issues/4062 |
| og:image:alt | Not sure this is a good idea, or even possible, but thought I'd throw it out there. In vanilla Rx, if I wanted to listen to a DOM event to do a typeahead or something I'd do something like: //get D... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | robwormald |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width