Skip to content

Commit d621fe8

Browse files
author
Will
committed
Add camelizeKeys static property to control attribute casing
Signed-off-by: Will <[email protected]>
1 parent be09b82 commit d621fe8

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/model.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class Model {
2828
static isJWTOwner: boolean = false;
2929
static jwt: string = null;
3030
static parentClass: typeof Model;
31+
static camelizeKeys: boolean = true;
3132

3233
id: string;
3334
temp_id: string;
@@ -209,7 +210,12 @@ export default class Model {
209210

210211
assignAttributes(attrs: Object) {
211212
for(var key in attrs) {
212-
let attributeName = camelize(key);
213+
let attributeName = key;
214+
215+
if (this.klass.camelizeKeys) {
216+
attributeName = camelize(key);
217+
}
218+
213219
if (key == 'id' || this.klass.attributeList[attributeName]) {
214220
this[attributeName] = attrs[key];
215221
}

test/fixtures.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class PersonWithExtraAttr extends Person {
1818
extraThing: string = attr({ persist: false });
1919
}
2020

21+
class PersonWithoutCamelizedKeys extends Person {
22+
static camelizeKeys = false;
23+
24+
first_name: string = attr();
25+
}
26+
2127
// Ensure setup() can be run multiple times with no problems
2228
// putting this here, otherwise relations wont be available.
2329
Config.setup();
@@ -95,6 +101,7 @@ export {
95101
Author,
96102
Person,
97103
PersonWithExtraAttr,
104+
PersonWithoutCamelizedKeys,
98105
Book,
99106
Genre,
100107
Bio,

test/unit/attributes-test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { sinon, expect } from '../test-helper';
2-
import { Person, Author } from '../fixtures';
2+
import { Person, Author, PersonWithoutCamelizedKeys } from '../fixtures';
33

44
describe('Model attributes', function() {
55
it('supports direct assignment', function() {
@@ -20,6 +20,12 @@ describe('Model attributes', function() {
2020
expect(person.firstName).to.eq('Joe');
2121
});
2222

23+
it('does not camlize underscored strings if camelization is disabled', function() {
24+
let person = new PersonWithoutCamelizedKeys({ first_name: 'Joe' });
25+
expect(person.firstName).to.eq(undefined);
26+
expect(person.first_name).to.eq('Joe');
27+
})
28+
2329
it('syncs with #attributes', function() {
2430
let person = new Person();
2531
expect(person.attributes).to.eql({});

0 commit comments

Comments
 (0)