Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection Sorting + Updating View #41

Closed
timarney opened this issue Oct 28, 2010 · 6 comments
Closed

Collection Sorting + Updating View #41

timarney opened this issue Oct 28, 2010 · 6 comments
Labels

Comments

@timarney
Copy link

I've just started playing with Backbone. Wondering what's the best approach for sorting and updating a view. I put together a little demo at http://backbone.line37.com/ but I'm sure there's a better way to do this:

alphaSort:function()
{
var alphabetical = Contacts.sortBy(function(contact) {
return contact.get("firstname").toLowerCase();
});

    $(".contact-list").html('');

    _.each(alphabetical, function(contact){ 
        App.addOne(contact);
    });

    return false;
}
@jashkenas
Copy link
Owner

You can use sortBy, as you are, or you can use a comparator function to keep models in a consistent sort order at all times:

http://documentcloud.github.com/backbone/#Collection-comparator

If you change the comparator function, you can call sort() to re-sort the set. If you're listening for "refresh" events, everything should update correctly, automatically.

Hope that helps...

@timarney
Copy link
Author

timarney commented Nov 1, 2010

Thanks - yes that makes sense. So in my case after each refresh, add etc... I would need to clear my view correct?

For example:
If my comparator is sorting models by first name and I'm already displaying Bob and Charlie and than I add Albert, I would need to clear the view and render again.

@jashkenas
Copy link
Owner

after each refresh, add etc... I would need to clear my view correct?

Yes, in the "refresh" event, you would clear and re-render the entire list of models.

If my comparator is sorting models by first name and I'm already
displaying Bob and Charlie and than I add Albert, I would need to
clear the view and render again.

No, you just have to insert new views at the appropriate location in the DOM. Instead of just a simple jQuery .append(), you might want to use insertBefore() or insertAfter(), to put Albert in the right place.

@timarney
Copy link
Author

timarney commented Nov 1, 2010

To do insert before or after - What would be the most efficient way to find it's neighbours in the Collection?

I was thinking if I bind to the add event and get the index of the newly inserted model I could check to see what the next index is or something like that.

@jashkenas
Copy link
Owner

Yep, you can do:

collection.indexOf(model)

... to get its position within the collection. From there, you can look up the adjacent elements, either by id, or by direct reference from the models. For the record, our code that handles this case looks like this:

var view          = new dc.ui.Project({model : model}).render();
var index         = Projects.indexOf(view.model);
var previous      = Projects.at(index - 1);
var previousView  = previous && previous.view;
if (index == 0 || !previous || !previousView) {
  $(this.projectList).prepend(view.el);
} else {
  $(previousView.el).after(view.el);
}

@timarney
Copy link
Author

timarney commented Nov 1, 2010

Very cool - thanks for the help.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants