Skip to content

Commit e770a82

Browse files
committed
Add findOne.
1 parent 9c2a21d commit e770a82

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/templates/js.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,43 @@
203203
})
204204
};
205205

206+
/**
207+
* Retrieve a single <?php echo $tableName; ?> with attributes matching the current <?php echo $tableName; ?>.
208+
*
209+
* @returns {*}
210+
*/
211+
var findOne = function()
212+
{
213+
var findAttributes = attributes;
214+
215+
findAttributes['limit'] = 1;
216+
217+
return doAJAX(
218+
'GET',
219+
'<?php echo $tablePlurals[$tableName]; ?>/?' + $.param(findAttributes)
220+
).then(function(result) {
221+
222+
if (result.data.length > 0) {
223+
var <?php echo $tableName; ?> = <?php echo $tablePhpNames[$tableName]; ?>(result.data[i].Id);
224+
225+
for (var name in result.data[i]) {
226+
if (result.data[i].hasOwnProperty(name)) {
227+
var setter = "set" + name;
228+
229+
if (<?php echo $tableName; ?>.hasOwnProperty(setter)) {
230+
<?php echo $tableName; ?>[setter](result.data[i][name]);
231+
}
232+
}
233+
}
234+
235+
return <?php echo $tableName; ?>;
236+
} else {
237+
return undefined;
238+
}
239+
240+
})
241+
};
242+
206243
/**
207244
* Perform the update or the create action for this <?php echo $tableName; ?>.
208245
*

0 commit comments

Comments
 (0)