Comments (14)
@anikolaev thank you for providing a code example. I'll take a closer look at the weekend 👍
from vuex-map-fields.
Would it not be better just to do this to get the entire object?
...mapFields(["myObject"])
Adding an asterisk * introduces a new thing that is not needed.
from vuex-map-fields.
@stijnjanmaat It should work with library's getField() too:
import { getField, updateField } from 'vuex-map-fields'
getNestedField(state) {
return getField(state.foo);
},
...
updateNestedField(state, field) {
updateField(state.foo, field);
},
from vuex-map-fields.
@shanehoban no it has not been implemented.
The idea with map fields is that it maps symbols on the vm root to paths in vuex.
What would be your expected outcome in terms of mapped symbols for ...mapFields('App', ['settings', 'config']),
?
Are you expecting that each item in the object is mapped to the vm root?
E.g. vm.debug
being mapped to config.debug
?
from vuex-map-fields.
I'll consider it, but I can't promise anything. Thank you for suggesting it.
from vuex-map-fields.
- 1 on this feature, it could be very very cool.
from vuex-map-fields.
@maoberlehner please consider this modification which implements the feature: anikolaev@fd0c1ad
Usage is almost the same as for multi-row (array) fields.
In *.vue:
<input v-model="selectedSeries.name"/>
<input v-model="selectedSeriesYAxis.name"/>
...
const mapSelectedSeriesFields = createHelpers({
getterType: 'forms/getSelectedSeriesField',
mutationType: 'forms/updateSelectedSeriesField'
}).mapObjectFields
...
export default {
computed: {
...mapSelectedSeriesFields({
selectedSeries: '*',
selectedSeriesYAxis: 'y_axis.*',
}),
}
As you can see I used it to access data for selected series using specific getter and setter for that and storing selected series index in store too. That is one of the solutions for dynamic properties but it is another issue.
from vuex-map-fields.
Nice @anikolaev, using your fork for now :). Maybe useful for somebody: you need a getter like this
getNestedField: state => path => {
if (path === '')
return state.foo;
return state.foo[path];
},
returning the whole foo
object when path
is empty. And a mutation like this:
updateNestedField(state, {path, value}) {
state.foo[path] = value;
},
from vuex-map-fields.
Hey all! Sorry for letting you wait – I just want to let you know that I've not forgotten about this. But I shifted my priorities to different projects for the last couple of weeks. I'll work on this as soon as I can free up some time.
Thank you for your patience!
from vuex-map-fields.
@maoberlehner I understand you. I've also shifted project and the new one unfortunately doesn't use Vue. But I hope you will find time to improve this lib as I found it very useful for complex forms.
from vuex-map-fields.
This is how I am using vuex-map-fields
in my project. The object.* is working but in a slightly different way (without syntactic sugar).
import store from '../store/index.js';
const getObjectByDotNotation = (a, b) =>
b.split('.')
.reduce((prev, cur) => prev[cur], a);
const flatArray = a => [].concat.apply([], a);
const keysMapForVuexFieldMap = (a, prefix, join = '.') => {
const keys = Object.keys(a);
const objects = keys.filter(k => (
typeof (a[k]) === 'object') && !(a[k] instanceof Array) && !(a[k] == null));
const ctxPrefix = z => (prefix ? `${prefix}${join}${z}` : z);
const fields = keys.filter(k => typeof (a[k]) !== 'object' || (a[k] instanceof Array) || (a[k] == null))
.map(k => ctxPrefix(k));
if (objects.length > 0) {
const y = objects.map(b => keysMapForVuexFieldMap(a[b], ctxPrefix(b)));
return flatArray(y)
.concat(fields.map(a => a));
}
return fields;
};
function getMapFieldsList(x, prefix) {
const a = getObjectByDotNotation(store(), x);
return keysMapForVuexFieldMap(a, prefix || null);
}
export default getMapFieldsList;
getMapFieldsList will return a list of fields in the state object which can be passed to mapFields
...mapFields([...VuexMapFieldService('state.UserProfile.form', 'form'), 'form', 'loading', 'error']),
Every property in UserProfile.form (UserProfile is a namespaced module) object will be returned as an array (even nested properties)
e.g
[ 'form.first_name', 'form.address.address_line_1', 'form.passport.passport_no']
keysMapForVuexFieldMap
is general enough to be applied on any object.
getMapFieldsList
is a wrapper which will use store as well.
from vuex-map-fields.
Yes that is exactly what I am looking for. I have a formData object where all the field data gets in. Then I want to bind it via v-model to this data. It is a plain Object with no nested elements as i break array structured down to and index identifier.
The form can get quite complex as it uses tabs and inline elements (composed from other relations) so a more generic approach would be appreciated.
from vuex-map-fields.
Has this been implemented, I can find anything about it, trying to have an object passed through like so (on strict mode it doesn't like it):
/* App.store.js */
import { getField, updateField } from 'vuex-map-fields';
const getDefaultAppState = () => {
return {
config: {
debug: process.env.VUE_APP_DEBUG_MODE || 'false',
},
settings: {
theme: 'dark-theme',
loadTimeout: 9857,
loadLimit: 69,
stickyScroll: true,
showBotComments: true,
showUsernames: true,
hideReplyIcons: false,
hidePostLabels: false,
disableConsoleLogs: false
}
}
}
// State object
const state = getDefaultAppState()
// Getter functions
const getters = {
getField,
}
// Actions
const actions = {}
// Mutations
const mutations = {
updateField,
RESET_APP (state) {
Object.assign(state, getDefaultAppState())
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
And in my component, I'd love to be able to do something like this:
computed: {
...mapFields('App', ['settings', 'config']),
},
Which would allow me to use v-model="settings.stickyScroll"
for example. Is there a way to do this (without throwing errors in strict mode that is)?
from vuex-map-fields.
Thanks @geoidesic.
To answer your question, I don't really understand how the internals work, but my expectation is that I will always be accessing object properties via dot notation eg.:
v-model="settings.stickyScroll"
so settings
as a whole becomes available, and not the properties of it without dot notation, e.g. if I reference stickyScroll
, this is never going to reference settings.stickyScroll
, if that makes sense
from vuex-map-fields.
Related Issues (20)
- How I can handle custom getter / setter for a single property. HOT 1
- createHelper concatenation goes haywire HOT 2
- nested arrays?
- Duplicate field names
- Can we use also actions and not only mutations?
- Support Vuex 4 HOT 3
- [PERFORMANCE] mapMultiRowFields is extremely slow with 100 items HOT 1
- cannot commit from programmatically + new feature action CRUD (nice)
- nuxt-vite support? HOT 2
- Use Vue.set in updateField to support reactive array watching HOT 1
- Namespaced Vuex modules with Folder structure possible?
- ...mapFields conditionally based on a prop value (question)
- mapMultiRowFields doesn't appear to support dot notation? HOT 1
- Pass namespace as a variable to mapFields
- How to handle array(with primitives) fields? - ["one", "two "]
- Reference a specific array index in mapFields?
- How to use ...mapFields in script setup and typescript?
- Something wrong when I use electron-vue HOT 1
- ...mapFields with CYpress componenet testing
- change "Twitter" to "𝕏 (Twitter)"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuex-map-fields.