Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from EDMdesigner/#20
Browse files Browse the repository at this point in the history
  • Loading branch information
gyulanemeth authored Aug 16, 2016
2 parents 3a675e5 + 6fd4a12 commit 916dc0f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.10",
"version": "0.1.11",
"name": "superdata",
"description": "A ligth weight data layer module motivated by extjs' data layer. It can be used with any client-side framework.",
"main": "src/superData.js",
Expand Down
31 changes: 30 additions & 1 deletion spec/model/modelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,43 @@ describe("model", function() {
model.list({}, {}, function(err) {
expect(err).toBe("belongsToValues has to have properties for references given in belongsTo");
});
model.load({}, {}, function(err) {
model.load(null, {}, function(err) {
expect(err).toBe("belongsToValues has to have properties for references given in belongsTo");
});
model.create({}, function(err) {
expect(err).toBe("modelValues has to have properties for references given in belongsTo");
});
});

it("should call callback with error if projectID's type isn't match to fields.projectID.type", function() {
model.list(
{},
{
projectID: "notANumber"
},
function(err) {
expect(err).toBe("Each property of belongsToValues has to match type with corresponding property of options.fields");
}
);
model.load(
null,
{
projectID: "notANumber"
},
function(err) {
expect(err).toBe("Each property of belongsToValues has to match type with corresponding property of options.fields");
}
);
model.create(
{
projectID: "notANumber"
},
function(err) {
expect(err).toBe("Each property of modelValues contained by belongsTo has to match type with corresponding property of options.fields");
}
);
});

it("should pass belongsToValues as parameter to proxy's functions", function() {
model.list({}, {
projectID: 2
Expand Down
20 changes: 19 additions & 1 deletion src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ module.exports = function createModel(options) {
// - afterChange

function checkReferences(belongsToValues) {
for(var i=0; i<belongsTo.length; i += 1) {
for(var i = 0; i < belongsTo.length; i += 1) {
if(!belongsToValues[belongsTo[i]]) {
return false;
}
}
return true;
}

function checkReferenceTypes(belongsToValues) {
for(var i = 0; i < belongsTo.length; i += 1) {
if(typeof belongsToValues[belongsTo[i]] !== fields[belongsTo[i]].type) {
return false;
}
}
return true;
}

function list(options, belongsToValues, callback) {
if(!callback) {
callback = belongsToValues;
Expand All @@ -66,6 +75,9 @@ module.exports = function createModel(options) {
if(!checkReferences(belongsToValues)) {
return callback("belongsToValues has to have properties for references given in belongsTo");
}
if(!checkReferenceTypes(belongsToValues)) {
return callback("Each property of belongsToValues has to match type with corresponding property of options.fields");
}
var filters = {};
for(var i=0; i<belongsTo.length; i += 1) {
filters[belongsTo[i]] = belongsToValues[belongsTo[i]];
Expand Down Expand Up @@ -102,6 +114,9 @@ module.exports = function createModel(options) {
if(!checkReferences(belongsToValues)) {
return callback("belongsToValues has to have properties for references given in belongsTo");
}
if(!checkReferenceTypes(belongsToValues)) {
return callback("Each property of belongsToValues has to match type with corresponding property of options.fields");
}
var filters = {};
for(var i=0; i<belongsTo.length; i += 1) {
filters[belongsTo[i]] = belongsToValues[belongsTo[i]];
Expand All @@ -124,6 +139,9 @@ module.exports = function createModel(options) {
if(!checkReferences(modelValues)) {
return callback("modelValues has to have properties for references given in belongsTo");
}
if(!checkReferenceTypes(modelValues)) {
return callback("Each property of modelValues contained by belongsTo has to match type with corresponding property of options.fields");
}
proxy.createOne(modelValues, function(err, result) {
if (err) {
return callback(err);
Expand Down

0 comments on commit 916dc0f

Please sign in to comment.