Hi, I’m trying to implement a list view which fetches emails from the server, caches them, and then fetches emails during a pull to refresh. In my template I show the email using ng-repeat:
<ion-refresher pulling-text="Pull to refresh" on-refresh="doRefresh()">
</ion-refresher>
<ion-list>
<ion-item ng-repeat="email in emails" ui-sref="app.actions({emailId: email.id})">
<p>{{email.from}}</p>
{{email.subject}}
</ion-item>
</ion-list>
In my controller I fetch the Emails from an email service in the beginning and then fetch new emails on the doRefresh() function:
.controller('InboxCtrl', function($scope, $http, Emails) {
$scope.emails = Emails.all();
$scope.remove = function(email) {
Emails.remove(email);
}
$scope.doRefresh = function() {
Emails.refresh().then(function(resp) {
console.log(resp);
// For JSON responses, resp.data contains the result
$scope.doneEmails = 6;
$scope.emails = resp;
}, function(err) {
console.error('ERR', err);
// err.status will contain the status code
})
.finally( function() {
$scope.$broadcast('scroll.refreshComplete');
console.log($scope.resource);
});
}
$scope.doneEmails = 7;
$scope.totalEmails = 18;
})
and then in the email service I actually fetch the emails from my server:
.factory('Emails', function($http) {
// Might use a resource here that returns a JSON array
// Some fake testing data
// Some fake testing data
var emails = [{
id: 0,
from: 'Ben Sparrow',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'First Email'
}, {
id: 1,
from: 'Jane Austen',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'New novel'
}, {
id: 2,
from: 'John Doe',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'Unknown Email'
}];
return {
all: function() {
return emails;
},
get: function(emailId) {
// Simple index lookup
return emails[emailId];
},
refresh: function() {
return $http.get('http://localhost:8080/emailAPI').then(function(response){
emails = response['data'];
return emails;
});
}
}
});
I expected that on pull-to-refresh, since I am writing to $scope, I should see the new emails returned from the server (will work later on the caching and appending pieces), but instead I get the error:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.
From what I can make of the error, its fetching and trying to replace, but running into duplicates? I tried making the ng-repeat line “email in emails track by email.id”, but no changes. The response from the server is:
[{
id: 3,
from: 'Ben Sparrow',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'Email 3'
}, {
id: 4,
from: 'Ben Sparrow',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'Email 4'
}, {
id: 5,
from: 'Ben Sparrow',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'Email 5'
}, {
id: 6,
from: 'Ben Sparrow',
to: 'Debnath Sinha',
time: '21/01/2014',
subject: 'Email 6'
}]
Do I need to do something explicit to render the new email list into the view?