Vue: Sync Modifier support object properties

Created on 28 Jul 2017  Â·  5Comments  Â·  Source: vuejs/vue

What problem does this feature solve?

Sync Modifier support object properties.
For example, I have a myObj object, it has a foo and a bar property.

myObj: {
  foo: 'foo',
  bar: 'bar'
}

This feature could use update:myObj.foo and update:myObj.bar to update the value.

this.$emit('update:myObj.foo', 'new-foo');
this.$emit('update:myObj.bar', 'new-bar');

What does the proposed API look like?

Currently, I need to use below format

<MyComponent :foo.sync="myObj.foo" :bar.sync="myObj.bar"></MyComponent>
props: ['foo', 'bar']
...
this.$emit('update:foo', 'new-foo');
this.$emit('update:bar', 'new-bar');



md5-3748bded9409f9aea18e0fa67a2f7a89



```javascript
props: ['myObj']
...
this.$emit('update:myObj.foo', 'new-foo');
this.$emit('update:myObj.bar', 'new-bar');

It becomes more simple and clear, especially when the object has many properties.

feature request

Most helpful comment

Although I can see the point of this proposal, I don't feel that it's particularly needed. In some way it encourages passing multiple values in an object to avoid having to specify multiple props. It is very easy to abuse object props by tucking more and more properties into it, making it some sort of "super prop". However, I think having an explicit list of props makes your component's interface easier to understand.

All 5 comments

You can use v-bind without directive arguments:

<MyComponent v-bind="myObj"></MyComponent>
...
props: ['foo', 'bar']
...

@javoski it seems more clear than before. So I can use like below code, right?

this.$emit('update:foo', 'new-foo');
this.$emit('update:bar', 'new-bar');

but why not just myObj.foo? Especially the object has many properties.

Although I can see the point of this proposal, I don't feel that it's particularly needed. In some way it encourages passing multiple values in an object to avoid having to specify multiple props. It is very easy to abuse object props by tucking more and more properties into it, making it some sort of "super prop". However, I think having an explicit list of props makes your component's interface easier to understand.

You can use v-bind without directive arguments:

<MyComponent v-bind="myObj"></MyComponent>
...
props: ['foo', 'bar']
...

using sync with an object work well.

<child :my-obj.sync = "myObj"/>

myObj take more than two key.
inner child

this.$emit("update:myObj",newObj)

who is more harder to understand? who is better? why?

You can use v-bind without directive arguments:

<MyComponent v-bind="myObj"></MyComponent>
...
props: ['foo', 'bar']
...

using sync with an object work well.

<child :my-obj.sync = "myObj"/>

myObj take more than two key.
inner child

this.$emit("update:myObj",newObj)

who is more harder to understand? who is better? why?

THis is great! works like a charm!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franciscolourenco picture franciscolourenco  Â·  3Comments

bdedardel picture bdedardel  Â·  3Comments

lmnsg picture lmnsg  Â·  3Comments

aviggngyv picture aviggngyv  Â·  3Comments

paceband picture paceband  Â·  3Comments