Closed tgemes closed 9 years ago
I think you're required to specify an id
property. From the readme:
*Note: Internally, $cachedResource keeps track of writes by bound params to ensure that it doesn't duplicate writes. If your bound param is null (say you're relying on the server to generate ids), then every write will replace the previous one. To avoid this undesirable scenario, simply ensure the id is set on the client, and you can safely ignore it on the server.
The examples all use id
, but I see your code is using _id
. Maybe that's the problem? This was just added about a month ago, so you could have easily missed it (https://github.com/goodeggs/angular-cached-resource/pull/78)
Thanks for the suggestion, you were completely right. I needed to add the _id to the accounts and then they didn't resolve to the same resource anymore. I also had to change the map function to explicitly return the promise of the save action. It works perfectly now. Thanks.
var promises = newAccounts.map(function(newAccount) {
newAccount._id = IdStore.nextId('Accounts');
var account = new Accounts(newAccount);
return account.$save().$promise;
});
Note: the IdStore is a client side helper service I made to make sure no two accounts get the same Id. It simply returns a new unique Id for the account.
I have an Accounts service which is an angular-cached-resource:
My createAll function takes an array and tries to post each element to the remote REST api:
Below is the karma test with example data. I'm expecting two POST requests made to the REST API since there are two elements in the array.
The issue is that I only get one Post request, and the promises on the Cached Resources resolve to the same object. Test results for the karma test:
If I run the same test with simply posting through the $http service, it works.
Please correct me if I'm using something wrong, I'm new to this module. I'd gladly do some more in-depth testing if needed. I'd hate to have to revert to $http, this module has been really great so far, thanks for the awesome work put into it.