Git Product home page Git Product logo

Comments (6)

jashkenas avatar jashkenas commented on May 12, 2024

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...

from backbone.

timarney avatar timarney commented on May 12, 2024

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.

from backbone.

jashkenas avatar jashkenas commented on May 12, 2024

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.

from backbone.

timarney avatar timarney commented on May 12, 2024

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.

from backbone.

jashkenas avatar jashkenas commented on May 12, 2024

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);
}

from backbone.

timarney avatar timarney commented on May 12, 2024

Very cool - thanks for the help.

from backbone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.