Last active
April 14, 2023 21:51
-
-
Save runspired/54ff8f95e348b529ba3e2098598e1f9f to your computer and use it in GitHub Desktop.
Transactional Bulk Endpoint
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
import Controller from '@ember/controller'; | |
import { action } from '@ember/object'; | |
import { tracked } from '@glimmer/tracking'; | |
import { inject as service } from '@ember/service'; | |
import { recordIdentifierFor } from '@ember-data/store'; | |
let count = 2; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
@service store; | |
all = this.store.peekAll('audit'); | |
@tracked records = [ | |
this.store.push({ | |
data: { | |
type: 'audit', | |
id: '1', | |
attributes: { name: 'Initial Audit' } | |
} | |
}) | |
]; | |
get savedRecords() { | |
return this.records.filter(r => !r.isNew); | |
} | |
get pendingRecords() { | |
return this.records.filter(r => r.isNew); | |
} | |
@action addNewRecord() { | |
const rCount = count++; | |
const audit = this.store.createRecord('audit', { | |
name: `new audit ${rCount}`, | |
count: rCount | |
}); | |
this.records.push(audit); | |
this.records = this.records; | |
} | |
async mySaveRequestMethod(records) { | |
const fetchedJson = await Promise.resolve({ | |
data: records.map(r => { | |
return { id: `${r.count}`, type: 'audit' }; | |
}) | |
}); | |
const identifiers = records.map(r => recordIdentifierFor(r)); | |
fetchedJson.data.forEach((resource, i) => { | |
resource.lid = identifiers[i].lid; | |
}); | |
this.store.push(fetchedJson); | |
} | |
@action async bulkSave() { | |
const pending = this.pendingRecords; | |
await this.mySaveRequestMethod(pending); | |
} | |
} |
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
import Model, { attr } from '@ember-data/model'; | |
export default class extends Model { | |
@attr name; | |
@attr count; | |
} |
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
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-data": "3.18.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment