Title: Add dynamic Widget/Component creation · Issue #389 · NativeScript/NativeScript · GitHub
Open Graph Title: Add dynamic Widget/Component creation · Issue #389 · NativeScript/NativeScript
X Title: Add dynamic Widget/Component creation · Issue #389 · NativeScript/NativeScript
Description: What’s the problem? The current UI definition paradigm requires us to know exactly which UI elements are needed before a Page creation. This is often _not_ the case for larger apps. Although data binding handles dynamic UI updating nicel...
Open Graph Description: What’s the problem? The current UI definition paradigm requires us to know exactly which UI elements are needed before a Page creation. This is often _not_ the case for larger apps. Although data b...
X Description: What’s the problem? The current UI definition paradigm requires us to know exactly which UI elements are needed before a Page creation. This is often _not_ the case for larger apps. Although data b...
Opengraph URL: https://github.com/NativeScript/NativeScript/issues/389
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add dynamic Widget/Component creation","articleBody":"### What’s the problem?\n\nThe current UI definition paradigm requires us to know exactly which UI elements are needed before a `Page` creation. This is often **_not**_ the case for larger apps. Although data binding handles dynamic UI updating nicely for tabular data and other small items, it is not the right solution for large dynamically generated UI compositions.\n### What do we have now?\n\nThe existing capabilities of {N} **_do**_ allow for us to specify isolated groupings of UI components (_or widgets_) referenced into the XML of the parent `Page`.\n\n```\n// Define our Widget in file \"xml-declaration/widget1\".\n\u003cStackLayout id=\"stackLayout\" \u003e\n \u003cLabel id=\"label1\" text=\"Label1\" /\u003e\n \u003cButton id=\"button1\" text=\"Button1\" /\u003e \n\u003c/StackLayout\u003e\n```\n\n```\n// Embed our custom Widget into our Page.\n\u003cPage xmlns:widgets=\"xml-declaration/widget1\"\u003e\n \u003cwidgets:Widget1 /\u003e\n\u003c/Page\u003e\n```\n### What do we need?\n\nWe need a dynamic widget builder, providing full control over **_when**_ a custom widget gets created.\n\nIdeally we’d expect its usage and API to be simple, perhaps something like:\n_(using the widget XML declared above)_\n\n```\nvar widget1 = viewBuilder.build({ name: ‘xml-declaration/widget1’ });\nsomeView.addChild(widget1);\n```\n\nCurrently we **_do**_ have the ability to dynamically create `Page` views/controllers from an XML file.\nThis is done via the `Frame` class using the static method `resolvePageFromEntry()`:\n\n```\nvar widget1 = frameCommon.resolvePageFromEntry({ moduleName: 'xml-declaration/widget1' });\nsomeView.addChild(widget1);\n```\n\nwhere the XML would be declared something like:\n\n```\n\u003cPage xmlns=\"http://www.nativescript.org/tns.xsd\" loaded=\"pageLoaded\" \u003e\n \u003cStackLayout id=\"stackLayout\" \u003e\n \u003cLabel id=\"label1\" text=\"Label1\" /\u003e\n \u003cButton id=\"button1\" text=\"Button1\" /\u003e \n \u003c/StackLayout\u003e\n\u003c/Page\u003e\n```\n\nNote that the widget must be wrapped in a `Page` element or this will **_not work**_!\nBut making every widget a `Page` would incur excessive overhead, and quite frankly you could create some nonsensical compositions, like a `Page` with a `StackedLayout` parent.\n\nThis is not the right solution.\n### Technical considerations\n\nThe main _benefit_ of having a `Page` as our root in a widget is contains the internal method `addCssFile()` which applies styling during the `resolvePageFromEntry()` call, where the css file name is dynamically determined from the target module name.\n\nThe main _drawback_ of having a `Page` root is that it knows about app Navigation, which a small widget absolutely should **_not**_. This abstraction is wrong for our widgets.\n\nCurrently the visible UI elements have different direct parent classes: `TextBase`, `Layout`, `ContentView`, and `View`. So introducing a generic builder which handles the automatic styling (like the `Page` does now) would mean they would all have to have a common base having the `addCssFile()` styling capability. Perhaps this could be retrofitted up into the shared-common base class `View`? Or perhaps some other class abstraction in between would be more correct.\n\nThen using the same builder call proposed before, we would be handed the correct (and stylized) widget, regardless of the outer container type specified in the XML:\n\n```\nvar widget1 = viewBuilder.build({ name: ‘xml-declaration/widget1’ });\nsomeView.addChild(widget1);\n```\n","author":{"url":"https://github.com/patrickseda","@type":"Person","name":"patrickseda"},"datePublished":"2015-07-03T21:46:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/389/NativeScript/issues/389"}
| 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:6a41771b-ad84-6298-fe45-7b4429c7747d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A492:28C0E5:4E48822:67521E2:6A5DA3E8 |
| html-safe-nonce | a1c2e9804bae123af6a7dc512145b14da6aadd51a7f8e7b84368d00e9aa231df |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDkyOjI4QzBFNTo0RTQ4ODIyOjY3NTIxRTI6NkE1REEzRTgiLCJ2aXNpdG9yX2lkIjoiNzYwNDUyMjgwNTIwMTA1MjY0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3733f4a4d870764332bcf5de314eb21eaa8a89b98adbb4a8bd94eb426aa45e30 |
| hovercard-subject-tag | issue:92940841 |
| 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/NativeScript/NativeScript/389/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fd78dc4803e1868a94c4fbb673098dd1ace76415c9c1eca5e7c66adadc6b9ec1/NativeScript/NativeScript/issues/389 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fd78dc4803e1868a94c4fbb673098dd1ace76415c9c1eca5e7c66adadc6b9ec1/NativeScript/NativeScript/issues/389 |
| og:image:alt | What’s the problem? The current UI definition paradigm requires us to know exactly which UI elements are needed before a Page creation. This is often _not_ the case for larger apps. Although data b... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | patrickseda |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/NativeScript/NativeScript git https://github.com/NativeScript/NativeScript.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 31492490 |
| octolytics-dimension-repository_nwo | NativeScript/NativeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31492490 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/NativeScript |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width