vuex-asr
Version:
* Version: 0.7.52 * License M.I.T. * [Online Documentation](https://vuex-asr.github.io/vuex-asr/) * author: Joris Wenting * email: vuex.asr@gmail.com * [linkedIn](https://www.linkedin.com/in/joriswenting/). * [contribute](https://vuex-asr.github.io/vuex-a
74 lines (68 loc) • 21.5 kB
HTML
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>binding multiple objects to a component | Vuex ASR</title>
<meta name="description" content="Automated store resolution for Vue.js">
<link rel="icon" href="../vue-logo.png">
<link rel="preload" href="/vuex-asr/assets/css/0.styles.ba1a25a1.css" as="style"><link rel="preload" href="/vuex-asr/assets/js/app.51a1fadf.js" as="script"><link rel="preload" href="/vuex-asr/assets/js/2.b4a928f2.js" as="script"><link rel="preload" href="/vuex-asr/assets/js/29.4ef76ef5.js" as="script"><link rel="prefetch" href="/vuex-asr/assets/js/10.d050881c.js"><link rel="prefetch" href="/vuex-asr/assets/js/11.4a0808ab.js"><link rel="prefetch" href="/vuex-asr/assets/js/12.3c479c42.js"><link rel="prefetch" href="/vuex-asr/assets/js/13.3250c310.js"><link rel="prefetch" href="/vuex-asr/assets/js/14.62c5e3bf.js"><link rel="prefetch" href="/vuex-asr/assets/js/15.1246035b.js"><link rel="prefetch" href="/vuex-asr/assets/js/16.f2a6d574.js"><link rel="prefetch" href="/vuex-asr/assets/js/17.c0c4ba46.js"><link rel="prefetch" href="/vuex-asr/assets/js/18.7f284ab7.js"><link rel="prefetch" href="/vuex-asr/assets/js/19.af7a8adf.js"><link rel="prefetch" href="/vuex-asr/assets/js/20.d5e4f501.js"><link rel="prefetch" href="/vuex-asr/assets/js/21.4073ea7c.js"><link rel="prefetch" href="/vuex-asr/assets/js/22.46346157.js"><link rel="prefetch" href="/vuex-asr/assets/js/23.4ac41cbb.js"><link rel="prefetch" href="/vuex-asr/assets/js/24.72b4b854.js"><link rel="prefetch" href="/vuex-asr/assets/js/25.3f626a44.js"><link rel="prefetch" href="/vuex-asr/assets/js/26.ed8ed3d3.js"><link rel="prefetch" href="/vuex-asr/assets/js/27.8c51c981.js"><link rel="prefetch" href="/vuex-asr/assets/js/28.86d49fe1.js"><link rel="prefetch" href="/vuex-asr/assets/js/3.2b8ad3c3.js"><link rel="prefetch" href="/vuex-asr/assets/js/30.7ad3ecff.js"><link rel="prefetch" href="/vuex-asr/assets/js/31.a8eb0654.js"><link rel="prefetch" href="/vuex-asr/assets/js/32.eb4c2bac.js"><link rel="prefetch" href="/vuex-asr/assets/js/33.00186259.js"><link rel="prefetch" href="/vuex-asr/assets/js/34.7b77e1ee.js"><link rel="prefetch" href="/vuex-asr/assets/js/35.26de21a8.js"><link rel="prefetch" href="/vuex-asr/assets/js/36.b062e03a.js"><link rel="prefetch" href="/vuex-asr/assets/js/37.bdead487.js"><link rel="prefetch" href="/vuex-asr/assets/js/38.2e2b47dd.js"><link rel="prefetch" href="/vuex-asr/assets/js/4.6cc4b15c.js"><link rel="prefetch" href="/vuex-asr/assets/js/5.51ffd98f.js"><link rel="prefetch" href="/vuex-asr/assets/js/6.f1224752.js"><link rel="prefetch" href="/vuex-asr/assets/js/7.3659af3a.js"><link rel="prefetch" href="/vuex-asr/assets/js/8.366b1b49.js"><link rel="prefetch" href="/vuex-asr/assets/js/9.af028c38.js">
<link rel="stylesheet" href="/vuex-asr/assets/css/0.styles.ba1a25a1.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vuex-asr/" class="home-link router-link-active"><!----> <span class="site-name">Vuex ASR</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/vuex-asr/" class="nav-link">Home</a></div><div class="nav-item"><a href="/vuex-asr/step-by-step-guide/hello-world-example.html" class="nav-link">Guide</a></div><div class="nav-item"><a href="/vuex-asr/helpers/cheat-sheet.html" class="nav-link">Cheatsheet</a></div><div class="nav-item"><a href="/vuex-asr/helpers/contribute.html" class="nav-link">Contribute</a></div><div class="nav-item"><a href="https://github.com/vuex-asr/vuex-asr" target="_blank" rel="noopener noreferrer" class="nav-link external">
Github
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/vuex-asr/" class="nav-link">Home</a></div><div class="nav-item"><a href="/vuex-asr/step-by-step-guide/hello-world-example.html" class="nav-link">Guide</a></div><div class="nav-item"><a href="/vuex-asr/helpers/cheat-sheet.html" class="nav-link">Cheatsheet</a></div><div class="nav-item"><a href="/vuex-asr/helpers/contribute.html" class="nav-link">Contribute</a></div><div class="nav-item"><a href="https://github.com/vuex-asr/vuex-asr" target="_blank" rel="noopener noreferrer" class="nav-link external">
Github
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Introduction</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuex-asr/" class="sidebar-link">vuex-asr</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Step by step guide</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuex-asr/step-by-step-guide/prerequisite.html" class="sidebar-link">prerequisite</a></li><li><a href="/vuex-asr/step-by-step-guide/installation.html" class="sidebar-link">installation</a></li><li><a href="/vuex-asr/step-by-step-guide/hello-world-example.html" class="sidebar-link">hello world example</a></li><li><a href="/vuex-asr/step-by-step-guide/getters-example.html" class="sidebar-link">hello world example with a getter</a></li><li><a href="/vuex-asr/step-by-step-guide/hello-world-of-2-way-binding.html" class="sidebar-link">hello world of 2 way binding</a></li><li><a href="/vuex-asr/step-by-step-guide/mutations.html" class="sidebar-link">mutations</a></li><li><a href="/vuex-asr/step-by-step-guide/actions.html" class="sidebar-link">actions</a></li><li><a href="/vuex-asr/step-by-step-guide/aliasing.html" class="sidebar-link">aliasing</a></li><li><a href="/vuex-asr/step-by-step-guide/namespacing.html" class="sidebar-link">namespacing</a></li><li><a href="/vuex-asr/step-by-step-guide/passing-bindings.html" class="sidebar-link">passing a binding to a child component</a></li><li><a href="/vuex-asr/step-by-step-guide/binding-multiple-objects-to-a-component.html" class="active sidebar-link">binding multiple objects to a component</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vuex-asr/step-by-step-guide/binding-multiple-objects-to-a-component.html#binding-multiple-items-convention-for-all-binders" class="sidebar-link">binding multiple items convention for all binders</a></li><li class="sidebar-sub-header"><a href="/vuex-asr/step-by-step-guide/binding-multiple-objects-to-a-component.html#message-board" class="sidebar-link">message board</a></li><li class="sidebar-sub-header"><a href="/vuex-asr/step-by-step-guide/binding-multiple-objects-to-a-component.html#namespacing-syntax-convention" class="sidebar-link">namespacing syntax convention</a></li></ul></li><li><a href="/vuex-asr/step-by-step-guide/binding-a-configuration.html" class="sidebar-link">binding a configuration</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Helpers</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuex-asr/helpers/asr-debug.html" class="sidebar-link">asr-debug</a></li><li><a href="/vuex-asr/helpers/working-with-the-test-suite.html" class="sidebar-link">working with the test-suite</a></li><li><a href="/vuex-asr/helpers/cheat-sheet.html" class="sidebar-link">cheatsheet</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="binding-multiple-objects-to-a-component"><a href="#binding-multiple-objects-to-a-component" aria-hidden="true" class="header-anchor">#</a> binding multiple objects to a component</h1> <p>In the <a href="/vuex-asr/step-by-step-guide/hello-world-example.html">hello world example</a> we saw that we can bind a variable from the store to the component with asr-bind-state. In the <a href="/vuex-asr/step-by-step-guide/hello-world-of-2-way-binding.html">hello world of 2 way binding example</a> we saw we can bind a model that can mutate a store object And we also learned we could make use of the <a href="/vuex-asr/step-by-step-guide/aliasing.html">alias</a> function to make components reusable. And in the <a href="/vuex-asr/step-by-step-guide/namespacing.html">namespacing</a> tutorial we saw how we could create our own code organisation with namespaces.</p> <p>Since VUEXasr is intended to make building large scale applications more easy let's see how we could bind multiple objects to a component.</p> <div class="tip custom-block"><p class="custom-block-title">codesandbox</p> <p><a href="https://codesandbox.io/s/manual-binding-multiple-objects-to-a-component-gulsx" target="_blank" rel="noopener noreferrer">binding multiple objects in a component example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <h2 id="binding-multiple-items-convention-for-all-binders"><a href="#binding-multiple-items-convention-for-all-binders" aria-hidden="true" class="header-anchor">#</a> binding multiple items convention for all binders</h2> <p>The examples in this chapter illustrate how you could implement binding multiple store items with <code>asr-bind-state</code>, but the convention is the same for all of the binders:</p> <ul><li>asr-bind-state</li> <li>asr-bind-getters</li> <li>asr-bind-mutations</li> <li>asr-bind-actions</li> <li>asr-bind-config</li></ul> <h2 id="message-board"><a href="#message-board" aria-hidden="true" class="header-anchor">#</a> message board</h2> <p>As I like to keep the examples as simple as possible let's reuse the previous example but now let's delegate multiple message objects into one component the MessageBoard</p> <p>First let's make a message board:</p> <div class="language-vue extra-class"><pre class="language-vue"><code> // src/components/MessageBoard.vue
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>A message from the root of the store<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>{{ message }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>Another message from the root of the store<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>{{ anotherMessage }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>A message from the User Module<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>{{ messageUser }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
</code></pre></div><p>notice that this component takes 3 variables:</p> <ul><li>message</li> <li>anotherMessage</li> <li>messageUser</li></ul> <p>Now let's see how we could bind objects to these variables:</p> <div class="language-vue extra-class"><div class="highlight-lines"><br><br><br><br><br><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><div class="highlighted"> </div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div><pre class="language-vue"><code> // src/App.vue
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>app<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>message-board</span>
<span class="token attr-name">asr-bind-state</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>
message,
anotherMessage,
USER/message AS messageUser<span class="token punctuation">"</span></span>
<span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>message-board</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text-input</span> <span class="token attr-name">asr-bind-state</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>message IS v-model<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text-input</span> <span class="token attr-name">asr-bind-state</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>anotherMessage IS v-model<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text-input</span> <span class="token attr-name">asr-bind-state</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>USER/message AS messageUser IS v-model<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">import</span> MessageBoard <span class="token keyword">from</span> <span class="token string">"./components/MessageBoard"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> TextInput <span class="token keyword">from</span> <span class="token string">"./components/TextInput"</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
name<span class="token punctuation">:</span> <span class="token string">"App"</span><span class="token punctuation">,</span>
components<span class="token punctuation">:</span> <span class="token punctuation">{</span>
MessageBoard<span class="token punctuation">,</span>
TextInput
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span>
</code></pre></div><p>Notice, the comma separated notation which is split into multiple lines for convenience and readability.</p> <h2 id="namespacing-syntax-convention"><a href="#namespacing-syntax-convention" aria-hidden="true" class="header-anchor">#</a> namespacing syntax convention</h2> <p>You could organise your bindings by separating them by a comma <code>,</code></p> <div class="language-vue extra-class"><pre class="language-vue"><code> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>some-component</span>
<span class="token attr-name">asr-bind-state</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>
message,
anotherMessage,
USER/messageUser,
USER/userObject AS user,
PHOTOS/recentPhotos
PHOTOS/likedPhotos<span class="token punctuation">"</span></span>
<span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>some-component</span><span class="token punctuation">></span></span>
</code></pre></div><p>In the example above these variables live in the global (root) namespace:</p> <ul><li>message</li> <li>anotherMessage</li></ul> <p>These live in the USER namespace:</p> <ul><li>messageUser</li> <li>userObject (aliased to 'user')</li></ul> <p>These live in the PHOTOS namespace:</p> <ul><li>recentPhotos</li> <li>likedPhotos</li></ul> <p>Well isn't that convenient? There is even another slick way to achieve the same functionality and even more with the asr-bind-config property coming up next 💪</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/vuex-asr/step-by-step-guide/passing-bindings.html" class="prev">passing a binding to a child component</a></span> <span class="next"><a href="/vuex-asr/step-by-step-guide/binding-a-configuration.html">binding a configuration</a>→
</span></p></div> </main></div><div class="global-ui"></div></div>
<script src="/vuex-asr/assets/js/app.51a1fadf.js" defer></script><script src="/vuex-asr/assets/js/2.b4a928f2.js" defer></script><script src="/vuex-asr/assets/js/29.4ef76ef5.js" defer></script>
</body>
</html>