Skip to content

Commit 8869954

Browse files
author
Lee Richmond
committed
Add decorator support
1 parent f037957 commit 8869954

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"chai-as-promised": "^6.0.0",
2727
"chai-things": "^0.2.0",
2828
"fetch-mock": "^5.10.0",
29-
"lodash-es": "^4.17.4",
29+
"lodash.clonedeep": "^4.5.0",
30+
"lodash.snakecase": "^4.1.1",
3031
"mocha": "^3.2.0",
3132
"shx": "^0.2.2",
3233
"sinon": "^2.1.0",
@@ -38,7 +39,6 @@
3839
"dependencies": {
3940
"es6-promise": "^4.0.5",
4041
"isomorphic-fetch": "^2.2.1",
41-
"lodash.clonedeep": "^4.5.0",
42-
"lodash.snakecase": "^4.1.1"
42+
"lodash-es": "^4.17.4"
4343
}
4444
}

src/attribute.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ export default class Attribute {
1212
static applyAll(klass: typeof Model) : void {
1313
this._eachAttribute(klass, (attr) => {
1414
klass.attributeList.push(attr.name);
15+
let instance = new klass();
16+
let descriptor = attr.descriptor();
17+
Object.defineProperty(klass.prototype, attr.name, descriptor);
1518

16-
Object.defineProperty(klass.prototype, attr.name, attr.getSet());
19+
let decorators = instance['__attrDecorators'] || [];
20+
decorators.forEach((d) => {
21+
if (d['attrName'] === attr.name) {
22+
d['decorator'](klass.prototype, attr.name, descriptor);
23+
}
24+
});
1725
});
1826
}
1927

@@ -35,10 +43,11 @@ export default class Attribute {
3543
}
3644

3745
// This returns the getters/setters for use on the *model*
38-
getSet() {
46+
descriptor() {
3947
let attr = this;
4048

4149
return {
50+
writeable: true,
4251
get() : any {
4352
return attr.getter(this);
4453
},

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ patchExtends();
66
import Config from './configuration';
77
import Model from './model';
88
import Attribute from './attribute';
9+
import attrDecorator from './util/attr-decorator';
910
import { hasMany, hasOne, belongsTo } from './associations';
1011

1112
const attr = function() : any {
1213
return new Attribute();
1314
}
1415

15-
export { Config, Model, attr, hasMany, hasOne, belongsTo, patchExtends };
16+
export { Config, Model, attr, attrDecorator, hasMany, hasOne, belongsTo, patchExtends };

src/model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import { CollectionProxy, RecordProxy } from './proxies';
88
import _extend from './util/extend';
99
import { camelize } from './util/string';
1010
import WritePayload from './util/write-payload';
11+
import IncludeDirective from './util/include-directive';
1112
import Request from './request';
13+
import * as _cloneDeep from './util/clonedeep';
14+
let cloneDeep: any = (<any>_cloneDeep).default || _cloneDeep;
15+
if (cloneDeep.default) {
16+
cloneDeep = cloneDeep.default;
17+
}
1218

1319
export default class Model {
1420
static baseUrl = 'http://please-set-a-base-url.com';

src/util/attr-decorator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function(decorator: Function) : Function {
2+
return function(target: any, attrName: string, descriptor: PropertyDescriptor) : void {
3+
if (!target['__attrDecorators']) target['__attrDecorators'] = [];
4+
target['__attrDecorators'].push({ attrName, decorator });
5+
}
6+
}

0 commit comments

Comments
 (0)