Skip to content

Commit

Permalink
Use extended MKCOL in backbone adapter
Browse files Browse the repository at this point in the history
This makes it possible to pre-set some properties without a subsequent
PROPPATCH call.
  • Loading branch information
Vincent Petry committed Mar 1, 2017
1 parent f17e391 commit 8011669
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
31 changes: 17 additions & 14 deletions core/js/oc-backbone-webdav.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@
});
}

function callPropPatch(client, options, model, headers, changed) {
function callPropPatch(client, options, model, headers) {
return client.propPatch(
options.url,
convertModelAttributesToDavProperties(changed || model.changed, options.davProperties),
convertModelAttributesToDavProperties(model.changed, options.davProperties),
headers
).then(function(result) {
if (isSuccessStatus(result.status)) {
Expand All @@ -199,21 +199,24 @@
}

function callMkCol(client, options, model, headers) {
// call MKCOL without data, followed by PROPPATCH
return client.request(
options.type,
var props = convertModelAttributesToDavProperties(model.attributes, options.davProperties);
if (!props['{DAV:}resourcetype']) {
props['{DAV:}resourcetype'] = '<d:collection/>';
}
return client.mkcol(
options.url,
headers,
null
props,
headers
).then(function(result) {
if (!isSuccessStatus(result.status)) {
if (_.isFunction(options.error)) {
options.error(result);
if (isSuccessStatus(result.status)) {
if (_.isFunction(options.success)) {
// pass the object's own values because the server
// does not return the updated model
options.success(model.toJSON());
}
return;
} else if (_.isFunction(options.error)) {
options.error(result);
}

callPropPatch(client, options, model, headers, model.attributes);
});
}

Expand All @@ -233,7 +236,7 @@
}

if (_.isFunction(options.success)) {
if (options.type === 'PUT' || options.type === 'POST' || options.type === 'MKCOL') {
if (options.type === 'PUT' || options.type === 'POST') {
// pass the object's own values because the server
// does not return anything
var responseJson = result.body || model.toJSON();
Expand Down
20 changes: 13 additions & 7 deletions core/js/tests/specs/oc-backbone-webdavSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ describe('Backbone Webdav extension', function() {
});

it('creates the Webdav collection with MKCOL', function() {
var mkcolStub = sinon.stub(dav.Client.prototype, 'mkcol');
mkcolStub.returns(deferredRequest.promise());
var model = new NodeModel({
id: 'davcol'
});
Expand All @@ -658,15 +660,17 @@ describe('Backbone Webdav extension', function() {
lastName: 'World',
});

expect(davClientRequestStub.calledOnce).toEqual(true);
expect(davClientRequestStub.getCall(0).args[0])
.toEqual('MKCOL');
expect(davClientRequestStub.getCall(0).args[1])
expect(mkcolStub.calledOnce).toEqual(true);
expect(mkcolStub.getCall(0).args[0])
.toEqual('http://example.com/owncloud/remote.php/dav/davcol');
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
expect(mkcolStub.getCall(0).args[1])
.toEqual({
'{http://owncloud.org/ns}first-name': 'Hello',
'{http://owncloud.org/ns}last-name': 'World',
'{DAV:}resourcetype': '<d:collection/>'
});
expect(mkcolStub.getCall(0).args[2]['X-Requested-With'])
.toEqual('XMLHttpRequest');
expect(davClientRequestStub.getCall(0).args[3])
.toEqual(null);

deferredRequest.resolve({
status: 201,
Expand All @@ -678,6 +682,8 @@ describe('Backbone Webdav extension', function() {

expect(model.id).toEqual('davcol');
expect(model.isNew()).toEqual(false);

mkcolStub.restore();
});
it('updates Webdav collection properties with PROPPATCH', function() {
var model = new NodeModel({
Expand Down

0 comments on commit 8011669

Please sign in to comment.