0

I am trying to filter some data from my collection. I am using where method for the job but it is returning an empty array. Here is the code.

Model:

return Backbone.Model.extend({
            urlRoot: server_url + "tasks",
            defaults: {
                'id': null,
                'title': '',
                'description': '',
                'deadline': null,
                'priority': 1,
                'status': 1,
                'key': '',
                'priority_name': '',
                'status_name': ''
            }
    });

Collection:

return Backbone.Collection.extend({
        url: server_url + "tasks",
        model: TaskModel
    });

And using it like:

var taskList = new TaskList();
taskList.fetch({
          data: $.param({key: $.cookie('SID')}),
          success: function(collection, response){
            if(response.error){
              window.location.replace('#logout');
            }
          }
        });
taskList.where({status: 1});

taskList have all the data in it. It is not empty. I have tried many combinations but hard luck every time.

I have also consulted from following posts but same result.

Backbone collection where clause with OR condition

Filter backbone collection by attribute value

toJSON on Backbone.Collection#where?

What I am missing here?

Community
  • 1
  • 1
Tausif Khan
  • 2,228
  • 9
  • 40
  • 51
  • 1
    If you're initializing the collection and then querying the data on the next line, how is the data getting into the collection? If you're `fetch`ing it in the constructor, then this is a concurrency issue - the `fetch` call has not yet returned by the time you query it. – jevakallio Mar 04 '13 at 08:38
  • 1
    Are you sure that when you call the `where` function the collection has already been fetched? Try this: `taskList.fetch({ success: function(){ taskList.where({status:1}); }});` – Ingro Mar 04 '13 at 08:47
  • I have edited the post to show the fetch process – Tausif Khan Mar 04 '13 at 10:36
  • I have also checked the 'where' in success function but still the empty array :( – Tausif Khan Mar 04 '13 at 10:57

1 Answers1

1

There doesn't appear to be anything wrong with the code you've posted, but then you're not showing us the population of your Collection. Remember that Backbone.Collection.where simply takes a literal and returns an array of matched Backbone.Model's. I have taken your code and simplified it to demonstrate a working example: http://jsfiddle.net/CK3Hz/

Edited as per comments below

Backbone.Collection.fetch's success callback receives (collection, response, options) arguments. If you are to perform the where on collection, this should work.

I have provided another jsfiddle to demonstrate using event propagation which I assume is your end goal.

juco
  • 6,331
  • 3
  • 25
  • 42
  • The code you have posted seems to be the same in sense. Is there any other thing which I am doing wrong? – Tausif Khan Mar 04 '13 at 10:38
  • Yes, as already mentioned @Ingro above, you are calling `where` without knowing whether the `taskList` has data. Try moving `where` inside the `success` callback. If this is not feasible you could consider [triggering an event](http://backbonejs.org/#Events-trigger) inside `success` to something else that performs the `where` – juco Mar 04 '13 at 10:52
  • I have checked it moving it in success callback. But again empty array :( – Tausif Khan Mar 04 '13 at 10:58