It's also worth noting that you can create new lists passing deferreds, so you could just as easily use new can.List(Cities.findAll) or otherwise use new Cities.List({ state: 'CA'}) to create a Cities.List that will add in items when resolved.
Also, the main issue with the approach presented above is that it'll always return a new list instance, and so it'll always make an ajax request, and it'll never return the same list instance.
A better approach would be to do this within a component, and make a compute that generates a deferred, and to listen to that and update your listen. There are lots of options as to how to do this.
For example:
- var Contact = can.Component.extend({
- tag: 'contact',
- scope: {
- query: function() {
- return Cities.findAll({ state: this.attr('state') });
- }
- },
- events: {
- '{query} change': function() {
- var query = this.scope.attr('query');
- this.scope.attr('cities' , new Cities.List(query));
- }
- }
- });