dimanche 30 août 2015

Non-enumerable properties

all instances of Ember.Object has non-enumerable properties that start with double underscores like __ember_meta, __next_super, __proto__ and so forth.

They are defined like this

 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);

And I have an Email value object, on which I tried to replicate this technic

// 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);
  }
});

and within initialization (inside init) everything looks correct

Object.keys(this)
["address", "label"]
Object.getOwnPropertyNames(this)
["__ember1440930504837", "__nextSuper", "__ember_meta__", "address", "label", "__skipValidation"]

but not later, when I try to save a model with array of the emails - __skipValidation is enumerable again :(

// 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"]

How to prevent property from becoming enumerable again?




Aucun commentaire:

Enregistrer un commentaire