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

global: general improvements #24

Merged
Merged
Show file tree
Hide file tree
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
63 changes: 25 additions & 38 deletions src/invenio-records-js/controllers/InvenioRecordsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
$rootScope.$broadcast('invenio.records.loading.start');
// Assign the model
vm.invenioRecordsModel = angular.copy(record);
// Assign endpoints
vm.invenioRecordsEndpoints = angular.merge(
{},
endpoints
);

// Assign the args
vm.invenioRecordsArgs = angular.merge(
{},
vm.invenioRecordsArgs,
args
);
// Assign endpoints
vm.invenioRecordsEndpoints = angular.merge(
{},
endpoints
);

if (Object.keys(links).length > 0) {
$rootScope.$broadcast(
Expand Down Expand Up @@ -131,13 +132,17 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
function getEndpoints(){
var deferred = $q.defer();
if (angular.isUndefined(vm.invenioRecordsEndpoints.self)) {
// Prepare the request
var request = InvenioRecordsAPI.prepareRequest(
vm.invenioRecordsEndpoints.initialization,
'POST',
{},
vm.invenioRecordsArgs,
vm.invenioRecordsEndpoints
);
// If the action url doesnt exists request it
InvenioRecordsAPI.request({
method: 'POST',
url: vm.invenioRecordsEndpoints.initialization,
data: {},
headers: vm.invenioRecordsArgs.headers || {}
}).then(function success(response) {
InvenioRecordsAPI.request(request)
.then(function success(response) {
// Upadate the endpoints
$rootScope.$broadcast(
'invenio.records.endpoints.updated', response.data.links
Expand All @@ -155,38 +160,21 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
}

/**
* Do a data massage before sending with request
* @memberof InvenioRecordsCtrl
* @function cleanData
*/
function cleanData() {
var _data = angular.merge({}, {metadata: vm.invenioRecordsModel});
var unwatend = [[null], [{}], '', [undefined]];
angular.forEach(_data.metadata, function(value, key) {
angular.forEach(unwatend, function(_value) {
if (angular.equals(_value, value)) {
delete _data.metadata[key];
}
});
});
return _data;
}

/**
* Make the API request with the _data payload
* Make the API request for the requested action
* @memberof InvenioRecordsCtrl
* @function makeActionRequest
* @param {String} type - The action type (any existing key from ``links``).
* @param {String} method - The method (POST, PUT, DELETE).
*/
function makeActionRequest(type, method) {
var _data = cleanData();
return InvenioRecordsAPI.request({
url: vm.invenioRecordsEndpoints[type],
method: (method || 'PUT').toUpperCase(),
data: _data,
headers: vm.invenioRecordsArgs.headers || {}
});
var request = InvenioRecordsAPI.prepareRequest(
vm.invenioRecordsEndpoints[type],
method,
vm.invenioRecordsModel,
vm.invenioRecordsArgs,
vm.invenioRecordsEndpoints
);
return InvenioRecordsAPI.request(request);
}

/**
Expand Down Expand Up @@ -429,7 +417,6 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
}
}


// Attach fuctions to the scope

// Action handler
Expand Down
3 changes: 1 addition & 2 deletions src/invenio-records-js/directives/invenioRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function invenioRecords() {
* @param {InvenioRecordsCtrl} vm - Invenio records controller.
*/
function link(scope, element, attrs, vm) {

// Upadate parameters
// Get extra template parameters
var templateParams = {
Expand All @@ -72,6 +71,7 @@ function invenioRecords() {
templateParams,
extraParams
);

// Get the endpoints for schemas
var endpoints = {
form: attrs.form,
Expand All @@ -80,7 +80,6 @@ function invenioRecords() {
};
// Get the record object
var record = JSON.parse(attrs.record || '{}');

// Spread the love of initialization
scope.$broadcast(
'invenio.records.init', args, endpoints, record, links
Expand Down
64 changes: 64 additions & 0 deletions src/invenio-records-js/services/InvenioRecordsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,72 @@ function InvenioRecordsAPI($http, $q) {
return request(args);
}

/**
* Clean the data from unwanted values
* @memberof InvenioRecordsAPI
* @param {Object} data - Provided by ``extra-params.data``.
* @param {Object} unwanted - A list with unwanted values.
*/
function cleanData(data, unwanted) {
var _unwantend = unwanted || [[null], [{}], '', [undefined]];
angular.forEach(data, function(value, key) {
angular.forEach(_unwantend, function(_value) {
if (angular.equals(_value, value)) {
delete data[key];
}
});
});
return data;
}

/**
* Prepare the request data
* @memberof InvenioRecordsAPI
* @param {Object} model -The data model.
* @param {Object} extraParams - Provided by ``extra-params``.
* @param {Object} endpoints - Endpoints defined during the initialization.
*/
function getData(model, extraParams, endpoints) {
// Prepare the data for the request
// Someone could use decorators and change the data
var data = angular.merge(
{},
extraParams.data || {},
cleanData(model)
);
// Add the schema if is not already defined
if (data.$schema === undefined && endpoints.schema !== undefined) {
data.$schema = endpoints.schema;
}
return data;
}

/**
* Prepare the request object
* @memberof InvenioRecordsAPI
* @param {String} url - The request url.
* @param {String} method - The request method.
* @param {Object} model -The data model.
* @param {Object} extraParams - Provided by ``extra-params``.
* @param {Object} endpoints - Endpoints defined during the initialization.
* @returns {Object} requestObject
*/
function prepareRequest(url, method, model, extraParams, endpoints) {
// build the requestObject
var requestObject = {
url: url,
method: method,
headers: extraParams.headers || {},
data: getData(model, extraParams, endpoints)
};
return requestObject;
}

return {
cleanData: cleanData,
get: get,
getData: getData,
prepareRequest: prepareRequest,
request: request,
};
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/invenio-records-js/controllersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,20 @@ describe('Unit: testing controllers', function() {
// Location should be
expect($location.url()).to.be.equal('/');
});

it('should return the data clean', function() {
var dirty = {
first: [null],
second: [{}],
third: '',
forth: [undefined],
newyork: 'Jessica Jones',
metropolis: 'Harley Quinn'
};
var clean = InvenioRecordsAPI.cleanData(dirty);
expect(clean).to.deep.equal({
newyork: 'Jessica Jones',
metropolis: 'Harley Quinn'
});
});
});