Skip to content

Commit

Permalink
Add findOne.
Browse files Browse the repository at this point in the history
  • Loading branch information
JASchilz committed Nov 8, 2016
1 parent 9c2a21d commit e770a82
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/templates/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,43 @@
})
};

/**
* Retrieve a single <?php echo $tableName; ?> with attributes matching the current <?php echo $tableName; ?>.
*
* @returns {*}
*/
var findOne = function()
{
var findAttributes = attributes;

findAttributes['limit'] = 1;

return doAJAX(
'GET',
'<?php echo $tablePlurals[$tableName]; ?>/?' + $.param(findAttributes)
).then(function(result) {

if (result.data.length > 0) {
var <?php echo $tableName; ?> = <?php echo $tablePhpNames[$tableName]; ?>(result.data[i].Id);

for (var name in result.data[i]) {
if (result.data[i].hasOwnProperty(name)) {
var setter = "set" + name;

if (<?php echo $tableName; ?>.hasOwnProperty(setter)) {
<?php echo $tableName; ?>[setter](result.data[i][name]);
}
}
}

return <?php echo $tableName; ?>;
} else {
return undefined;
}

})
};

/**
* Perform the update or the create action for this <?php echo $tableName; ?>.
*
Expand Down

0 comments on commit e770a82

Please sign in to comment.