Make Realm object properties enumerable
This commit is contained in:
parent
7a54b0a256
commit
4109c86343
|
@ -19,7 +19,6 @@ function create(realmId, info) {
|
||||||
let schema = info.schema;
|
let schema = info.schema;
|
||||||
let constructor = (registeredConstructors[realmId] || {})[schema.name];
|
let constructor = (registeredConstructors[realmId] || {})[schema.name];
|
||||||
let object = constructor ? Object.create(constructor.prototype) : {};
|
let object = constructor ? Object.create(constructor.prototype) : {};
|
||||||
let props = {};
|
|
||||||
|
|
||||||
object[keys.realm] = realmId;
|
object[keys.realm] = realmId;
|
||||||
object[keys.id] = info.id;
|
object[keys.id] = info.id;
|
||||||
|
@ -28,14 +27,13 @@ function create(realmId, info) {
|
||||||
schema.properties.forEach((prop) => {
|
schema.properties.forEach((prop) => {
|
||||||
let name = prop.name;
|
let name = prop.name;
|
||||||
|
|
||||||
props[name] = {
|
Object.defineProperty(object, name, {
|
||||||
|
enumerable: true,
|
||||||
get: util.getterForProperty(name),
|
get: util.getterForProperty(name),
|
||||||
set: util.setterForProperty(name),
|
set: util.setterForProperty(name),
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.defineProperties(object, props);
|
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,14 @@ bool ObjectSetProperty(JSContextRef ctx, JSObjectRef jsObject, JSStringRef jsPro
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
|
void ObjectPropertyNames(JSContextRef ctx, JSObjectRef jsObject, JSPropertyNameAccumulatorRef propertyNames) {
|
||||||
return;
|
Object *obj = RJSGetInternal<Object *>(jsObject);
|
||||||
|
|
||||||
|
for (auto &prop : obj->object_schema.properties) {
|
||||||
|
JSStringRef propertyName = RJSStringForString(prop.name);
|
||||||
|
JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
|
||||||
|
JSStringRelease(propertyName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JSClassRef RJSObjectClass() {
|
JSClassRef RJSObjectClass() {
|
||||||
|
|
|
@ -253,6 +253,24 @@ module.exports = BaseTest.extend({
|
||||||
TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1);
|
TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1);
|
||||||
TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2);
|
TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2);
|
||||||
},
|
},
|
||||||
|
testEnumerablePropertyNames: function() {
|
||||||
|
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), new ArrayBuffer()];
|
||||||
|
var realm = new Realm({schema: [schemas.BasicTypes]});
|
||||||
|
var object;
|
||||||
|
|
||||||
|
realm.write(function() {
|
||||||
|
object = realm.create('BasicTypesObject', basicTypesValues);
|
||||||
|
});
|
||||||
|
|
||||||
|
var propNames = schemas.BasicTypes.properties.map(function(prop) { return prop.name; });
|
||||||
|
TestCase.assertArraysEqual(Object.keys(object), propNames, 'Object.keys');
|
||||||
|
|
||||||
|
for (var key in object) {
|
||||||
|
TestCase.assertEqual(key, propNames.shift());
|
||||||
|
}
|
||||||
|
|
||||||
|
TestCase.assertEqual(propNames.length, 0);
|
||||||
|
},
|
||||||
testDataProperties: function() {
|
testDataProperties: function() {
|
||||||
var realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
|
var realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
|
||||||
var object;
|
var object;
|
||||||
|
|
|
@ -35,7 +35,7 @@ function runTests() {
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.log('- ' + testName);
|
console.log('- ' + testName);
|
||||||
console.warn(e);
|
console.warn(e.message);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (testSuite.afterEach) {
|
if (testSuite.afterEach) {
|
||||||
|
|
Loading…
Reference in New Issue