Skip to content

Commit

Permalink
Fixes #27.
Browse files Browse the repository at this point in the history
Stable Version 1.0.0-alpha.5-8.
  • Loading branch information
jmdobry committed Dec 6, 2014
1 parent 1f18617 commit 90e81d9
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 107 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
##### 1.0.0-alpha.5-8 - 05 December 2014

###### Backwards compatible API changes
- #27 - Properly resolve parent params for generating the URL

##### 1.0.0-alpha.5-7 - 05 December 2014

###### Backwards compatible API changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Unlike Backbone and Ember Models, js-data does not require the use of getters an

Supporting relations, computed properties, model lifecycle control and a slew of other features, js-data is the tool for giving your data the respect it deserves.

__Latest Release:__ [1.0.0-alpha.5-7](https://github.com/js-data/js-data/releases/tag/1.0.0-alpha.5-7)
__Latest Release:__ [1.0.0-alpha.5-8](https://github.com/js-data/js-data/releases/tag/1.0.0-alpha.5-8)

js-data is pre-release. The API is subject to change, though the current api is well tested.

Expand Down
63 changes: 37 additions & 26 deletions dist/js-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @author Jason Dobry <jason.dobry@gmail.com>
* @file js-data.js
* @version 1.0.0-alpha.5-7 - Homepage <http://www.js-data.io/>
* @version 1.0.0-alpha.5-8 - Homepage <http://www.js-data.io/>
* @copyright (c) 2014 Jason Dobry
* @license MIT <https://github.com/js-data/js-data/blob/master/LICENSE>
*
Expand Down Expand Up @@ -3321,6 +3321,7 @@ function defineResource(definition) {
if (relation.parent) {
def.parent = modelName;
def.parentKey = relation.localKey;
def.parentField = relation.localField;
}
});
});
Expand All @@ -3331,36 +3332,46 @@ function defineResource(definition) {
}
}

def.getEndpoint = function (attrs, options) {
options = DSUtils.deepMixIn({}, options);
var parent = this.parent;
var parentKey = this.parentKey;
var item;
var endpoint;
var thisEndpoint = options.endpoint || this.endpoint;
var parentDef = definitions[parent];
delete options.endpoint;
options = options || {};
def.getEndpoint = function (id, options) {
options.params = options.params || {};
if (parent && parentKey && parentDef && options.params[parentKey] !== false) {
if (DSUtils.isNumber(attrs) || DSUtils.isString(attrs)) {
item = _this.get(this.name, attrs);
}
if (DSUtils.isObject(attrs) && parentKey in attrs) {
delete options.params[parentKey];
endpoint = DSUtils.makePath(parentDef.getEndpoint(attrs, options), attrs[parentKey], thisEndpoint);
} else if (item && parentKey in item) {
delete options.params[parentKey];
endpoint = DSUtils.makePath(parentDef.getEndpoint(attrs, options), item[parentKey], thisEndpoint);
} else if (options && options.params[parentKey]) {
endpoint = DSUtils.makePath(parentDef.getEndpoint(attrs, options), options.params[parentKey], thisEndpoint);

var item;
var parentKey = def.parentKey;
var endpoint = options.hasOwnProperty('endpoint') ? options.endpoint : def.endpoint;
var parentField = def.parentField;
var parentDef = definitions[def.parent];
var parentId = options.params[parentKey];

if (parentId === false || !parentKey || !parentDef) {
if (parentId === false) {
delete options.params[parentKey];
}
}
if (options.params[parentKey] === false) {
return endpoint;
} else {
delete options.params[parentKey];

if (DSUtils.isNumber(id) || DSUtils.isString(id)) {
item = def.get(id);
} else if (DSUtils.isObject(id)) {
item = id;
}

if (item) {
parentId = parentId || item[parentKey] || (item[parentField] ? item[parentField][parentDef.idAttribute] : null);
}

if (parentId) {
delete options.endpoint;
var _options = {};
DSUtils.forOwn(options, function (value, key) {
_options[key] = value;
});
var e = DSUtils.makePath(parentDef.getEndpoint(parentId, DSUtils._(parentDef, _options)), parentId, endpoint);
return e;
} else {
return endpoint;
}
}
return endpoint || thisEndpoint;
};

// Remove this in v0.11.0 and make a breaking change notice
Expand Down
4 changes: 2 additions & 2 deletions dist/js-data.min.js

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions karma.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var lifecycle = {};
// Helper globals
var fail = function (msg) {
if (msg instanceof Error) {
console.log(msg.stack);
console.error(msg.stack);
} else {
assert.equal('should not reach this!: ' + msg, 'failure');
}
Expand Down Expand Up @@ -68,7 +68,6 @@ beforeEach(function () {
cb(null, attrs);
};
lifecycle.beforeDestroy = function (resourceName, attrs, cb) {
console.log(resourceName, attrs, cb);
lifecycle.beforeDestroy.callCount += 1;
cb(null, attrs);
};
Expand Down Expand Up @@ -331,7 +330,7 @@ beforeEach(function () {
requests.push(xhr);
};
} catch (err) {
console.log(err);
console.error(err);
}
});

Expand All @@ -340,6 +339,6 @@ afterEach(function () {
try {
this.xhr.restore();
} catch (err) {
console.log(err);
console.error(err);
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-data",
"description": "Robust, framework-agnostic in-memory data store.",
"version": "1.0.0-alpha.5-7",
"version": "1.0.0-alpha.5-8",
"homepage": "http://www.js-data.io",
"repository": {
"type": "git",
Expand Down
61 changes: 36 additions & 25 deletions src/datastore/sync_methods/defineResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function defineResource(definition) {
if (relation.parent) {
def.parent = modelName;
def.parentKey = relation.localKey;
def.parentField = relation.localField;
}
});
});
Expand All @@ -93,36 +94,46 @@ function defineResource(definition) {
}
}

def.getEndpoint = function (attrs, options) {
options = DSUtils.deepMixIn({}, options);
var parent = this.parent;
var parentKey = this.parentKey;
var item;
var endpoint;
var thisEndpoint = options.endpoint || this.endpoint;
var parentDef = definitions[parent];
delete options.endpoint;
options = options || {};
def.getEndpoint = function (id, options) {
options.params = options.params || {};
if (parent && parentKey && parentDef && options.params[parentKey] !== false) {
if (DSUtils.isNumber(attrs) || DSUtils.isString(attrs)) {
item = _this.get(this.name, attrs);
}
if (DSUtils.isObject(attrs) && parentKey in attrs) {
delete options.params[parentKey];
endpoint = DSUtils.makePath(parentDef.getEndpoint(attrs, options), attrs[parentKey], thisEndpoint);
} else if (item && parentKey in item) {
delete options.params[parentKey];
endpoint = DSUtils.makePath(parentDef.getEndpoint(attrs, options), item[parentKey], thisEndpoint);
} else if (options && options.params[parentKey]) {
endpoint = DSUtils.makePath(parentDef.getEndpoint(attrs, options), options.params[parentKey], thisEndpoint);

var item;
var parentKey = def.parentKey;
var endpoint = options.hasOwnProperty('endpoint') ? options.endpoint : def.endpoint;
var parentField = def.parentField;
var parentDef = definitions[def.parent];
var parentId = options.params[parentKey];

if (parentId === false || !parentKey || !parentDef) {
if (parentId === false) {
delete options.params[parentKey];
}
}
if (options.params[parentKey] === false) {
return endpoint;
} else {
delete options.params[parentKey];

if (DSUtils.isNumber(id) || DSUtils.isString(id)) {
item = def.get(id);
} else if (DSUtils.isObject(id)) {
item = id;
}

if (item) {
parentId = parentId || item[parentKey] || (item[parentField] ? item[parentField][parentDef.idAttribute] : null);
}

if (parentId) {
delete options.endpoint;
var _options = {};
DSUtils.forOwn(options, function (value, key) {
_options[key] = value;
});
var e = DSUtils.makePath(parentDef.getEndpoint(parentId, DSUtils._(parentDef, _options)), parentId, endpoint);
return e;
} else {
return endpoint;
}
}
return endpoint || thisEndpoint;
};

// Remove this in v0.11.0 and make a breaking change notice
Expand Down
2 changes: 1 addition & 1 deletion test/both/datastore/sync_methods/ejectAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('DS#ejectAll', function () {
try {
store.ejectAll('post', { where: { age: 33 } });
} catch (err) {
console.log(err.stack);
console.error(err.stack);
}
assert.doesNotThrow(function () {
store.ejectAll('post', { where: { age: 33 } });
Expand Down
44 changes: 24 additions & 20 deletions test/browser/datastore/async_methods/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('DS#create', function () {
assert.deepEqual(JSON.stringify(store.get('post', 5)), JSON.stringify(p1));
done();
}).catch(function (err) {
console.log(err.stack);
console.error(err.stack);
done('should not have rejected');
});

Expand All @@ -22,7 +22,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts');
assert.equal(_this.requests[0].method, 'post');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({ author: 'John', age: 30 }));
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(p1));
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(p1));
}, 30);
});
it('should create an item and save it to the server but not inject the result', function (done) {
Expand All @@ -47,7 +47,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts');
assert.equal(_this.requests[0].method, 'post');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({ author: 'John', age: 30 }));
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(p1));
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(p1));
}, 30);
});
it('should work with the upsert option', function (done) {
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[1].url, 'http://test.js-data.io/posts');
assert.equal(_this.requests[1].method, 'post');
assert.equal(_this.requests[1].requestBody, DSUtils.toJson({ author: 'Sue', age: 70, id: 6 }));
_this.requests[1].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(p2));
_this.requests[1].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(p2));
}, 30);
}).catch(function (err) {
console.error(err.stack);
Expand All @@ -91,7 +91,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts/5');
assert.equal(_this.requests[0].method, 'put');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({ author: 'John', age: 30, id: 5 }));
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(p1));
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(p1));
}, 30);
});
it('should create an item that includes relations, save them to the server and inject the results', function (done) {
Expand Down Expand Up @@ -126,7 +126,6 @@ describe('DS#create', function () {
assert.equal(store.get('user', 99).id, payload.id);
assert.isObject(store.get('user', 99).profile);
assert.equal(store.get('profile', 999).id, 999);
console.log(store.get('profile', 999).user);
assert.isObject(store.get('profile', 999).user);

store.find('user', 99); // should not trigger another http request
Expand All @@ -138,16 +137,20 @@ describe('DS#create', function () {
});

setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'http://test.js-data.io/user');
assert.equal(_this.requests[0].method, 'post');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({
name: 'Sally',
profile: {
email: 'sally@test.com'
}
}));
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(payload));
try {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'http://test.js-data.io/user');
assert.equal(_this.requests[0].method, 'post');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({
name: 'Sally',
profile: {
email: 'sally@test.com'
}
}));
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(payload));
} catch (err) {
done(err);
}
}, 30);
});
it('should handle nested resources', function (done) {
Expand Down Expand Up @@ -200,7 +203,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[2].url, 'http://test.js-data.io/comment');
assert.equal(_this.requests[2].method, 'post');
assert.equal(_this.requests[2].requestBody, DSUtils.toJson({ content: 'test', approvedBy: 4 }));
_this.requests[2].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(testComment2));
_this.requests[2].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(testComment2));
}, 30);
}).catch(function () {
done('Should not have failed!');
Expand All @@ -211,7 +214,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[1].url, 'http://test.js-data.io/user/4/comment');
assert.equal(_this.requests[1].method, 'post');
assert.equal(_this.requests[1].requestBody, DSUtils.toJson({ content: 'test' }));
_this.requests[1].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(testComment2));
_this.requests[1].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(testComment2));
}, 30);
}).catch(function () {
done('Should not have failed!');
Expand All @@ -222,7 +225,7 @@ describe('DS#create', function () {
assert.equal(_this.requests[0].url, 'http://test.js-data.io/user/4/comment');
assert.equal(_this.requests[0].method, 'post');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({ content: 'test', approvedBy: 4 }));
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson(testComment));
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson(testComment));
}, 30);
});
it('should find inverse links', function (done) {
Expand Down Expand Up @@ -255,12 +258,13 @@ describe('DS#create', function () {
organizationId: 77,
id: 88
}));
_this.requests[0].respond(200, {'Content-Type': 'application/json'}, DSUtils.toJson({
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, DSUtils.toJson({
organizationId: 77,
id: 88
}));
} catch (err) {
console.error(err.stack);
done(err);
}
}, 30);
});
Expand Down
Loading

0 comments on commit 90e81d9

Please sign in to comment.