修改noJson的实现逻辑
This commit is contained in:
parent
6c274bb7c6
commit
9a017e7606
@ -1,18 +1,32 @@
|
|||||||
import 'reflect-metadata'
|
import 'reflect-metadata'
|
||||||
|
import { singleton } from './singleton'
|
||||||
|
|
||||||
const noJsonMetadataKey = ('noJsonSet')
|
const noJsonMetadataKey = ('noJsonSet')
|
||||||
|
|
||||||
|
@singleton
|
||||||
|
export class NoJsonClass {
|
||||||
|
private noJsonPropSet: Set<string> = new Set()
|
||||||
|
|
||||||
|
public addKey(className: string, propertyKey: string) {
|
||||||
|
this.noJsonPropSet.add(className+'_'+propertyKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
public checkExist(className: string, propertyKey: string) {
|
||||||
|
return this.noJsonPropSet.has(className+'_'+propertyKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 这方法无效
|
|
||||||
* 在不需要toJson方法输出的字段上加上 @noJson
|
* 在不需要toJson方法输出的字段上加上 @noJson
|
||||||
* @return {{(target: Function): void, (target: Object, propertyKey: (string | symbol)): void}}
|
* @return {{(target: Function): void, (target: Object, propertyKey: (string | symbol)): void}}
|
||||||
*/
|
*/
|
||||||
export function noJson() {
|
export function noJson() {
|
||||||
return Reflect.metadata(noJsonMetadataKey, !0)
|
// return Reflect.metadata(noJsonMetadataKey, !0)
|
||||||
|
return function(target: Object, propertyKey: string) {
|
||||||
|
console.log(target)
|
||||||
|
new NoJsonClass().addKey(target.constructor.name, propertyKey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkJson(target: any, propertyKey: string) {
|
export function checkJson(target: any, propertyKey: string) {
|
||||||
let val = Reflect.getMetadata(noJsonMetadataKey, target, propertyKey)
|
return !new NoJsonClass().checkExist(target.constructor.modelName, propertyKey)
|
||||||
let s = Reflect.getMetadata('typegoose:properties', target, propertyKey)
|
|
||||||
return !val
|
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ export abstract class BaseModule extends FindOrCreate {
|
|||||||
let result: any = {}
|
let result: any = {}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
for (let key in this._doc) {
|
for (let key in this._doc) {
|
||||||
if (jsonExcludeKeys.indexOf(key) == -1) {
|
if (checkJson(this, key+'' ) && jsonExcludeKeys.indexOf(key) == -1) {
|
||||||
result[key] = this[key]
|
result[key] = this[key]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,6 @@ import { noJson } from '../../decorators/nojson'
|
|||||||
class PuzzleClass extends BaseModule {
|
class PuzzleClass extends BaseModule {
|
||||||
@prop()
|
@prop()
|
||||||
public question: string
|
public question: string
|
||||||
|
|
||||||
@prop()
|
@prop()
|
||||||
public a1: string
|
public a1: string
|
||||||
@prop()
|
@prop()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user