Thanks, Rod. That is helpful, but, I'm not trying to create a new entity. I
want to get an already existing entity and update it. I did a little more
work and figured out that what I need is to use a third parameter in the
getEntity() callback, like so:
client.getEntity(identifier, function(err, response,
entity) {
if (err) {
alert("Error getting entity to be updated");
} else {
var restaurant = entity;
var city = $("#edit-city-field").val();
restaurant.set("city", city);
restaurant.save(function(err) {
if (err) {
alert("Error updating entity");
}
});
loadList();
history.back();
}
});
- Dave
On Sat, Aug 30, 2014 at 10:34 AM, Rod Simpson <[email protected]> wrote:
> Dave,
>
> Take a look at this readme file:
>
>
> https://github.com/rodsimpson/incubator-usergrid/blob/master/sdks/html5-javascript/README.md
>
> You don’t want to use this (you can, but it is harder):
>
> var restaurant = new Usergrid.Entity( {
>
> Instead, the SDK has factory methods in the client for creating entities
> and collections, so what you would do is this:
>
> var options = {
>
> type:’restaurant',
>
> name:’Joes'
>
> }
>
>
> client.createEntity(options, function (err, restaurant) {
>
> if (err) {
>
> //Error - Dog not created
>
> } else {
>
> //Success - Dog was created
>
> //now var restaurant is a locally scoped entity object
>
> var name = restaurant.get(‘name’);
>
> }
>
> });
>
>
>
>
>
>
>
> --
> Rod Simpson
> @rockerston <https://twitter.com/rockerston>
> rodsimpson.com
>
> On August 30, 2014 at 8:15:56 AM, Dave ([email protected]) wrote:
>
> I'm working on an example Usergrid app with the JavaScript SDK and jQuery
> Mobile and I have an update function that looks like this:
>
> function update() {
>
> var name = $("#edit-name-field").val();
> var identifier = {
> name: name,
> type: "restaurant"
> };
>
> client.getEntity(identifier, function(err, response) {
> if (err) {
> alert("Error getting entity to be updated");
>
> } else {
>
> // CREATE ENTITY FROM DATA
> var restaurant = new Usergrid.Entity( {
> client: client,
> data: response.getEntities()[0]
> });
>
> var city = $("#edit-city-field").val();
> restaurant.set("city", city);
> restaurant.save(function(err) {
> if (err) {
> alert("Error updating entity");
> }
> });
> loadList();
> history.back();
> }
> });
>
> }
>
> The above code works for me, but I have a question about it.
>
> It took me a while to figure out that the response.getEntities() method
> does not return Entities, instead it returns "data" objects and you must
> use those to construct Entities if you want to use Entity methods such as
> set() and save().
>
> My question is: why does't getEntities() return Entities? Is this a bug or
> am I using the SDK incorrectly in some way.
>
> Thanks,
> Dave
>
>