Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some changes to make it work on facilityUser creation #4

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions kolibri/core/assets/src/api_resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,31 @@ class Model {
this.synced = false;

// force IDs to always be strings - this should be changed on the server-side too
this.attributes[this.resource.idKey] = String(this.attributes[this.resource.idKey]);
if ('id' in data) {
this.attributes[this.resource.idKey] = String(this.attributes[this.resource.idKey]);
}

// Keep track of any unresolved promises that have been generated by async methods of the Model
this.promises = [];
}

getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name.concat('='))) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}


/**
* Method to fetch data from the server for this particular model.
* @param {object} params - an object of parameters to be parsed into GET parameters on the
Expand Down Expand Up @@ -100,18 +119,21 @@ class Model {
} else {
this.synced = false;
let url;
let method;
let clientObj;
if (this.id) {
// If this Model has an id, then can do a PATCH against the Model
url = this.url;
method = 'PATCH';
clientObj = { path: url, method: 'PATCH' };
} else {
// Otherwise, must POST to the Collection endpoint to create the Model
url = this.resource.collectionUrl();
method = 'POST';
const csrftoken = this.getCookie('csrftoken');
clientObj = { path: url, entity: payload,
headers: { 'Content-Type': 'application/json',
'X-CSRFToken': csrftoken } };
}
// Do a save on the URL.
client({ path: url, method }).then((response) => {
client(clientObj).then((response) => {
const oldId = this.id;
// Set the retrieved Object onto the Model instance.
this.set(response.entity);
Expand All @@ -121,8 +143,8 @@ class Model {
}
// Flag that the Model has been fetched.
this.synced = true;
// Resolve the promise with the attributes of the Model.
resolve(this.attributes);
// Resolve the promise with the Model.
resolve(this);
// Clean up the reference to this promise
this.promises.splice(this.promises.indexOf(promise), 1);
}, (response) => {
Expand Down