Ember.Object
的所有实例都有不可枚举的属性,以__ember_meta
,__next_super
,__proto__
等双下划线开头。
它们的定义如下
export var META_DESC = {
writable: true,
configurable: true,
enumerable: false,
value: null
};
var EMBER_META_PROPERTY = {
name: '__ember_meta__',
descriptor: META_DESC
};
//....
obj.__defineNonEnumerable(EMBER_META_PROPERTY);
我有一个Email
值对象,我尝试复制此技术
// lib/validatable-object.js
import Ember from 'ember';
const SKIP_VALIDATION = {
name: '__skipValidation',
descriptor: {
writable: true,
configurable: true,
enumerable: false,
value: null
}
};
export default Ember.Object.extend({
init: function() {
this.__defineNonEnumerable(SKIP_VALIDATION);
this._super(...arguments);
}
});
并在初始化内(init
内)一切看起来都正确
Object.keys(this)
["address", "label"]
Object.getOwnPropertyNames(this)
["__ember1440930504837", "__nextSuper", "__ember_meta__", "address", "label", "__skipValidation"]
但不是以后,当我尝试保存包含emails
- __skipValidation
数组的模型时,可以再次枚举:(
// inside serialize() function
Object.keys(snapshot._attributes.emails[0])
["address", "label", "__skipValidation"]
Object.getOwnPropertyNames(snapshot._attributes.emails[0])
["address", "__nextSuper", "__ember1440931027338", "validLabel", "label", "__skipValidation", "__ember_meta__", "validAddress"]
如何防止财产再次变为可枚举?
更新:
我将configurable: true
更改为configurable: false
,Ember.js将错误更改为https://github.com/emberjs/ember.js/blob/bdf819565c6dba1728146e69fd8b495c735902e1/packages/ember-metal/lib/watch_key.js#L81