Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve data set/delete API #5050

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ declare interface Component {
$mount: (el?: Element | string, hydrating?: boolean) => Component;
$forceUpdate: () => void;
$destroy: () => void;
$set: (obj: Array<mixed> | Object, key: mixed, val: mixed) => void;
$delete: (obj: Object, key: string) => void;
$set: <T>(target: Object | Array<T>, key: string | number, val: T) => T;
$delete: <T>(target: Object | Array<T>, key: string | number) => void;
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
$on: (event: string | Array<string>, fn: Function) => Component;
$once: (event: string, fn: Function) => Component;
Expand Down
4 changes: 2 additions & 2 deletions flow/global-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ declare interface GlobalAPI {
util: Object;

extend: (options: Object) => Function;
set: (obj: Object, key: string, value: any) => void;
delete: (obj: Object, key: string) => void;
set: <T>(target: Object | Array<T>, key: string | number, value: T) => T;
delete: <T>(target: Object| Array<T>, key: string | number) => void;
nextTick: (fn: Function, context?: Object) => void;
use: (plugin: Function | Object) => void;
mixin: (mixin: Object) => void;
Expand Down
38 changes: 19 additions & 19 deletions src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,27 @@ export function defineReactive (
* triggers change notification if the property doesn't
* already exist.
*/
export function set (obj: Array<any> | Object, key: any, val: any) {
if (Array.isArray(obj)) {
obj.length = Math.max(obj.length, key)
obj.splice(key, 1, val)
export function set (target: Array<any> | Object, key: any, val: any): any {
if (Array.isArray(target)) {
target.length = Math.max(target.length, key)
target.splice(key, 1, val)
return val
}
if (hasOwn(obj, key)) {
obj[key] = val
return
if (hasOwn(target, key)) {
target[key] = val
return val
}
const ob = obj.__ob__
if (obj._isVue || (ob && ob.vmCount)) {
const ob = target.__ob__
if (target._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
'Avoid adding reactive properties to a Vue instance or its root $data ' +
'at runtime - declare it upfront in the data option.'
)
return
return val
}
if (!ob) {
obj[key] = val
return
target[key] = val
return val
}
defineReactive(ob.value, key, val)
ob.dep.notify()
Expand All @@ -218,23 +218,23 @@ export function set (obj: Array<any> | Object, key: any, val: any) {
/**
* Delete a property and trigger change if necessary.
*/
export function del (obj: Array<any> | Object, key: any) {
if (Array.isArray(obj)) {
obj.splice(key, 1)
export function del (target: Array<any> | Object, key: any) {
if (Array.isArray(target)) {
target.splice(key, 1)
return
}
const ob = obj.__ob__
if (obj._isVue || (ob && ob.vmCount)) {
const ob = target.__ob__
if (target._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
'Avoid deleting properties on a Vue instance or its root $data ' +
'- just set it to null.'
)
return
}
if (!hasOwn(obj, key)) {
if (!hasOwn(target, key)) {
return
}
delete obj[key]
delete target[key]
if (!ob) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/modules/observer/observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ describe('Observer', () => {
data
}).$mount()
expect(vm.$el.outerHTML).toBe('<div>1</div>')
Vue.set(data, 'a', 2)
expect(Vue.set(data, 'a', 2)).toBe(2)
waitForUpdate(() => {
expect(vm.$el.outerHTML).toBe('<div>2</div>')
expect('Avoid adding reactive properties to a Vue instance').not.toHaveBeenWarned()
Vue.delete(data, 'a')
}).then(() => {
expect('Avoid deleting properties on a Vue instance').toHaveBeenWarned()
expect(vm.$el.outerHTML).toBe('<div>2</div>')
Vue.set(data, 'b', 123)
expect(Vue.set(data, 'b', 123)).toBe(123)
expect('Avoid adding reactive properties to a Vue instance').toHaveBeenWarned()
}).then(done)
})
Expand Down