You can do

var saveData = {}
for (var key) in item {saveData[key] = item[key]}
and then use saveData as data: saveData.

But you can also probably just use the item itself. AFAIK, you don't have
to stringify the data going into an $http call (I don't, at least).

So:

$http.({ ...

  data: {'$set': item}
})

right?


On Mon, May 26, 2014 at 4:28 AM, Marc B <[email protected]> wrote:

> Hello,
> I have a angular app that lets users update items against a REST NoSQL
> backend (MongoLab)  I wonder how I can update an item without having to
> enumerate all keys - value pairs in a $http.put() call?
>
> My code:
>
> View:
>
> <tr ng-repeat-start="item in items | orderByPriority">
>     <td><input type="text" ng-model="item.startDate"></td>
>     <td><input type="text" ng-model="item.endDate"></td>
>     [...more fields...]
>     <td><span ng-click="updateItem(item)">&nbsp;</span></td>
>
>
> itemController:
>
> // update an item
> $scope.updateItem = function(item){
>     dataService.updateItem(item)
>     .success(...)
>     .error(...)
>
>
> dataservice
>     service.updateItem = function(item){
>         return $http({
>             method: 'PUT',
>             url: itemsRef + '/' + item.id + '?apiKey=' + apiKey,
>             data: JSON.stringify({"$set" : {
>                 "startDate":item.startDate,
>                 "endDate":item.endDate,
>                 "name":item.name,
>                 "notes":item.notes,
>                 "fileSrc":item.fileSrc,
>                 "fileName":item.fileName,
>                 "fileType":item.fileType,
>                 "fileSize":item.fileSize
>                 }}),
>             contentType: "application/json"
>         });
>     };
>
> This works but I prefer not to explicitly enumerate all the fields...so
> that when later a field is added I will not have to update this
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to