-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create module transfer-state * Rename module * Update readme * Fix text in readme
- Loading branch information
1 parent
9cd5e4e
commit d4adc10
Showing
4 changed files
with
77 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { PLATFORM_ID, Inject, NgModule } from '@angular/core'; | ||
import { isPlatformServer } from '@angular/common'; | ||
import * as ajax from 'devextreme/core/utils/ajax'; | ||
import * as deferred from 'devextreme/core/utils/deferred'; | ||
import { TransferState, makeStateKey, BrowserTransferStateModule } from '@angular/platform-browser'; | ||
import { ServerTransferStateModule } from '@angular/platform-server'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
ServerTransferStateModule, | ||
BrowserTransferStateModule | ||
] | ||
}) | ||
|
||
export class DxServerTransferStateModule { | ||
constructor(private state: TransferState, @Inject(PLATFORM_ID) private platformId: any) { | ||
let that = this; | ||
|
||
ajax.inject({ | ||
sendRequest: function(...args) { | ||
let key = makeStateKey(that.generateKey(args)), | ||
сaсhedData = that.state.get(key, null as any); | ||
|
||
if (isPlatformServer(that.platformId)) { | ||
let result = this.callBase.apply(this, args); | ||
result.always((data, status) => { | ||
let dataForCache = { | ||
data: data, | ||
status: status | ||
}; | ||
that.state.set(key, dataForCache as any); | ||
}); | ||
return result; | ||
} else { | ||
if (сaсhedData) { | ||
let d = new deferred.Deferred(); | ||
d.resolve(сaсhedData.data, сaсhedData.status); | ||
that.state.set(key, null as any); | ||
|
||
return d.promise(); | ||
} | ||
return this.callBase.apply(this, args); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
generateKey(args) { | ||
let keyValue = ''; | ||
for (let key in args) { | ||
if (typeof args[key] === 'object') { | ||
let objKey = this.generateKey(args[key]); | ||
keyValue += key + objKey; | ||
} else { | ||
keyValue += key + args[key]; | ||
} | ||
} | ||
|
||
return keyValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters