Skip to content

Commit 6ee88e1

Browse files
committed
docs: improve useBindToQueryString examples
1 parent 3838c7a commit 6ee88e1

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

docs/stacks/vue/layers/models.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Binds a value on an object, or the value of a ref, to the query string.
8383
function useBindToQueryString(ref: Ref<any>, options: BindToQueryStringOptions): void
8484

8585
interface BindToQueryStringOptions {
86+
queryKey?: string;
8687
mode?: 'push' | 'replace'
8788
parse?: (value: string) => any
8889
stringify?: (value: any) => string
@@ -99,13 +100,8 @@ Binds a value on an object, or the value of a ref, to the query string.
99100

100101
**Auto-detection features:**
101102
- **ListParameters**: When binding to a `ListParameters` object (or any object with `page` and `pageSize` properties), numeric fields are automatically parsed using `parseInt` without needing to specify a `parse` function.
102-
- **ListViewModel $ properties**: When binding to properties starting with `$` (e.g., `$page`, `$pageSize`, `$search`), the query key automatically drops the `$` prefix (e.g., `$page` becomes `page` in the URL). Numeric properties are automatically parsed using `parseInt`.
103-
104-
::: tip Migration Note
105-
Existing code using `{ parse: parseInt }` for `ListParameters` properties or manual `queryKey` specification for `$` properties can be simplified by removing these options - the auto-detection will handle them automatically.
106-
:::
107-
108-
If the object being bound to has `$metadata`, information from that metadata will be used to serialize and parse values to and from the query string. Otherwise, the `stringify` option (default: `String(value)`) will be used to serialize the value, and the `parse` option (if provided) will be used to parse the value from the query string.
103+
- **ListViewModel $ properties**: When binding to the ListViewModel shorthand parameter properties (e.g., `$page`, `$pageSize`, `$search`), the query key automatically drops the `$` prefix (e.g., `$page` becomes `page` in the URL). Numeric properties are automatically parsed using `parseInt`.
104+
- **Models and DataSources**: When binding to a property on a [Model](#model-interfaces), [ViewModel](./viewmodels.md#viewmodels), or [Data Source](#data-sources), `parse` and `stringify` are auto-configured.
109105

110106
- **Example**
111107

@@ -135,16 +131,32 @@ Binds a value on an object, or the value of a ref, to the query string.
135131
```ts
136132
import { useBindToQueryString } from 'coalesce-vue';
137133

138-
// Bind pagination information to the query string (auto-detects numeric parsing):
134+
// Bind to a property on an object:
139135
const list = new PersonListViewModel();
140-
useBindToQueryString(list.$params, 'pageSize');
141-
142-
// Bind ListViewModel shorthand parameters (auto-detects query key and numeric parsing):
143136
useBindToQueryString(list, '$page');
144137
useBindToQueryString(list, '$search');
138+
useBindToQueryString(list.$params, 'pageSize');
139+
140+
const dataSource = new PersonListViewModel.DataSources.MySource();
141+
useBindToQueryString(dataSource, 'myParameter');
145142

143+
// Bind to a `ref` ('tab' is the querystring key)
146144
const activeTab = ref("1")
147-
useBindToQueryString(activeTab, 'activeTab');
145+
useBindToQueryString(activeTab, 'tab');
146+
147+
// With more complex options:
148+
const enumTab = ref<MyEnum>(MyEnum.Home)
149+
useBindToQueryString(enumTab, {
150+
queryKey: 'tab',
151+
stringify: (val: MyEnum) => MyEnum[val],
152+
parse: (val: string) => {
153+
if (val && val in MyEnum) {
154+
const enumVal = MyEnum[val as keyof typeof MyEnum];
155+
return typeof enumVal === 'number' ? enumVal : null;
156+
}
157+
return null;
158+
},
159+
});
148160
```
149161

150162
</template>

0 commit comments

Comments
 (0)