-
-
Couldn't load subscription status.
- Fork 33.8k
Added support for properties which were created with defineProperty o… #1762
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
Changes from 4 commits
0b1be55 7953f5e 2e9db42 9e11b62 7fa6faa File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| var _ = require('../util') | ||
| var config = require('../config') | ||
| var Dep = require('./dep') | ||
| var arrayMethods = require('./array') | ||
| var arrayKeys = Object.getOwnPropertyNames(arrayMethods) | ||
| | @@ -172,11 +173,27 @@ function copyAugment (target, src, keys) { | |
| | ||
| function defineReactive (obj, key, val) { | ||
| var dep = new Dep() | ||
| var childOb = Observer.create(val) | ||
| | ||
| var target = { | ||
| val: val | ||
| } | ||
| | ||
| if (config.convertAllProperties) { | ||
| var property = Object.getOwnPropertyDescriptor(obj, key) | ||
| if (property && property.get && property.set) { | ||
| Object.defineProperty(target, 'val', { | ||
| get: _.bind(property.get, obj), | ||
| set: _.bind(property.set, obj) | ||
| }) | ||
| } | ||
| } | ||
| | ||
| var childOb = Observer.create(target.val) | ||
| Object.defineProperty(obj, key, { | ||
| enumerable: true, | ||
| configurable: true, | ||
| get: function metaGetter () { | ||
| var val = target.val | ||
| if (Dep.target) { | ||
| dep.depend() | ||
| if (childOb) { | ||
| | @@ -189,11 +206,11 @@ function defineReactive (obj, key, val) { | |
| } | ||
| } | ||
| } | ||
| return val | ||
| return target.val | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fine to just return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how I missed replacing this one out; I'll add a test condition which enforces only calling the underlying getter once. | ||
| }, | ||
| set: function metaSetter (newVal) { | ||
| if (newVal === val) return | ||
| val = newVal | ||
| if (newVal === target.val) return | ||
| target.val = newVal | ||
| childOb = Observer.create(newVal) | ||
| dep.notify() | ||
| } | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed it's possible for a property to only have getters, or only have setters. We should handle all three possible situations here. Probably need to procedurally build up the descriptor based on the presence of the getter and the setter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please bear with me, thanks for the effort so far :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries it's all good feedback.
I think the getter/setter stuff makes sense, but might as well also make it safely handle 'configurable' being false. For now I'll make it so it just leaves everything alone; let me know if you think some other behavior makes more sense.