Comments (13)
Right now you have to do it manually, but I think this is a nice feature addition to add to Restangular.
Right now you should find the item you're updating, remove it, and add the result of doing a put.
I'll add this to the next version.
I'm thinking in something like
collection.putElement(idx).then(function(updatedCollection) {
// Here you have the updated collection
});
from restangular.
That would be awesome indeed !
What do you mean by "do it manually" for now. Do you mean refreshing the items collection from the server ?
from restangular.
obj.put().then(function(updatedObj) {
array[idx] = updatedObj;
});
Edited for better code :)
from restangular.
That's what I meant. You remove old + add new element to collection.
from restangular.
So it's not that hard to do it now :)
from restangular.
Just added and landed in v 0.6.7.
You can do
// Params and headers are optional as usual
array.putElement(idx, params, headers).
from restangular.
Please let me know if this works out for you
from restangular.
I'm not sure about how I'm supposed to use this new method.
As far as I'm understanding the code, putElement is updating the array and PUT the element to the server.
In my app, I have a Controller that handles the modification of one item of my items collection. This controller is calling the putElement method on the items collection and is supposed to give feedback to the user if something goes wrong. However, my items collection is in another scope that handles the collection display.
When I pass the items collection to the modification controller, I can update the content of my item in place and then call the putElement methods. The items gets updated on the server, but my items collection on my first scope never gets updated....
from restangular.
Hey, so this putElement
method is actually inmutable like all of the rest of Restangular methods. Also, it returns a promise.
So, calling putElement
will return a promise of the new array, so what you can do is the following.
From your child controller / directive, receive as a parameter the collection of elements. Then just do the following in your controller.
$scope.collection.putElement(idx).then(function(newArray) {
$scope.collection = newArray;
}, function(response) {console.log("There was an error")} );
As you have scope prototypal inheritance, changing it here, will also change it in your parent controller and therefore will change the view as well.
Hope this works.
from restangular.
Hello,
I have been testing the putElement method in many ways but I can't make it works in my app.
Actually, I need to run the put or putElement method before I close my edit dialog, but I can't find a way to update my items list in my scope after closing the dialog.
Here is a simplified version of my CRUD app :
function ItemsController($scope, Restangular, $dialog) {
var items = $scope.items = Restangular.all("items").getList();
$scope.addItem = function() {
$dialog.open().then(function(newItem) {
//this is working fine
//$scope is updated successfully
items.push(newItem);
});
};
$scope.editItem = function(item) {
var index = items.$$v.indexOf(item);
$dialog.open().then(function(updatedItem) {
//this is not updating the $scope
items.splice(index, 1, updatedItem);
});
};
$scope.deleteItem = function(item) {
var index = items.$$v.indexOf(item);
$dialog.open().then(function() {
//this is not updating the $scope
items.splice(index, 1);
});
};
}
function NewItemController($scope, $dialog, Restangular) {
$scope.save = function() {
var items = Restangular.all("items");
items.post(item).then(function(newItem) {
$dialog.close(newItem);
}, function(error) {
//show error on dialog
});
};
$scope.cancel = function () {
$dialog.close();
};
}
function EditItemController($scope, $dialog, item) {
$scope.save = function(item) {
item.put().then(function(updatedItem) {
$dialog.close(updatedItem);
}, function(error) {
//show error on dialog
});
};
$scope.cancel = function () {
$dialog.close();
};
}
function DeleteItemController($scope, $dialog, item) {
$scope.save = function(item) {
item.delete().then(function(deletedItem) {
$dialog.close();
}, function(error) {
//show error on dialog
});
};
$scope.cancel = function () {
$dialog.close();
};
}
from restangular.
Hey,
When you do the getList
you are getting a promise that's why the splice doesn't work.
So do the following
// Change from this
var items = $scope.items = Restangular.all("items").getList();
// to this
var items = $scope.items = []
Restangular.all("items").getList().then(function(serverItems) {
items = serverItems;
$scope.items = serverItems.
});
That should do it :)
from restangular.
Let me know if this works for you.
from restangular.
this worked perfectly fine for me, thanks
from restangular.
Related Issues (20)
- Error when injecting restangular with angular 1.6.4 HOT 2
- Nested Services or Nested Configuration
- $remove() id as query parameter HOT 1
- new web app using restangular - www.connecttal.com -
- Element.plain() behaves differently compared to Retangular.stripRestangular
- How to make sync call?
- How to set array of object as parameters using customGET ?
- Did restangular support handling text/html or text/csv response?
- Response for getList SHOULD be an array and not an object or something else HOT 1
- make etag a strong etag when used in if-None-Match HOT 1
- Cancel request - Angular 1.5 HOT 1
- getList() response Json object loss some data
- How do I append query parameter with restangular.oneUrl()? HOT 1
- Details of HTTP request sent by put method ?
- Group by count
- addFullRequestInterceptor has empty headers object
- TypeError: Converting circular structure to JSON while <restangular-object>.put() HOT 1
- Multiple remove on Restangular
- Deprecated?
- Support DELETE method with body
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restangular.