GithubHelp home page GithubHelp logo

Comments (8)

sberryman avatar sberryman commented on May 3, 2024

So as of right now I've just decided to overwrite the appendHtml function.

Changed appendHtml to:

appendHtml: function(collectionView, itemView) {
  var itemIndex;
  itemIndex = collectionView.collection.indexOf(itemView.model);
  return collectionView.$el.insertAt(itemIndex, itemView.$el);
}

Then I have added the following to extend jQuery giving me insertAt

(function($) {
  return jQuery.fn.insertAt = function(index, element) {
    var lastIndex;
    if (index <= 0) return this.prepend(element);
    lastIndex = this.children().size();
    if (index >= lastIndex) return this.append(element);
    return $(this.children()[index - 1]).after(element);
  };
})(jQuery);

from backbone.marionette.

mxriverlynn avatar mxriverlynn commented on May 3, 2024

Honestly, I've never had to use a sorted collection so I guess I'm not surprised that this doesn't work right now.

It should, though. This is on my list of things to fix for the next release. I'm not sure when I'll have time to get to it, though. If you have a chance to dig in to it at all, I would really appreciate that. :)

from backbone.marionette.

sberryman avatar sberryman commented on May 3, 2024

That's what I figured. I'm going to stick with my work around until I can knock a few more items off the to-do list then I'll try and circle back around. I'm pretty sure it works by default in backbone so I'll probably just take a peek at how they implemented it and then send you a pull request based on that.

from backbone.marionette.

tabouassaleh avatar tabouassaleh commented on May 3, 2024

In my case, I typically sort collections by reverse chronological order. To do so, I overwrote appendHtml to use $.prepend instead of $.append, told my collection comparator to sort things in reverse, and also listen on the collection's change:timestamp event to trigger a manual sort when and item's timestamp is updated server-side (through Backbone.iosync/iobind).

Having this work correctly without such hacks would be very useful to me.

from backbone.marionette.

mxriverlynn avatar mxriverlynn commented on May 3, 2024

@tgcondor how would you make this to work, if you were writing your own views and code?

i'm not sure there's a good way to make this work except for doing exactly what you've described.

the only other way i know of would be to re-render the entire list every time something changes. and that's a bad idea from a performance perspective.

from backbone.marionette.

tabouassaleh avatar tabouassaleh commented on May 3, 2024

@derickbailey I probably shouldn't have used the words "without such hacks". What I really meant is to have support for this within Marionette, possibly enabled/disabled with a parameter, so that coders don't have to repeat the pattern. One thought is to given a CollectionView a sort_attribute field, which, when set, would add a comparator to the view's collection if it doesn't already have one and do the other necessary behavioural changes.

But if this is not a common-enough usecase, perhaps it would be sufficient to add to the wiki or readme a "recipe" for others to follow.

from backbone.marionette.

chancancode avatar chancancode commented on May 3, 2024

@derickbailey @tgcondor From what I understand, Backbone.Collection only guarantee the integrity of the ordering with respect to model insertion and removal. It does not resort the collection based on the changes in the model attributes... it would be the programmer's responsibility to manually listen to those changes somewhere and call theCollection.sort() manually (which triggers a reset event).

With that in mind, it doesn't seem that difficult to implement this in Marionette's CollectionView.

I think the only addition we need here is to maintain an array of the itemViews we have inserted into the DOM. On every add event, the collection will also pass along the index (via options.index). As long as we respect this and insert the itemView in the correct position in the DOM (something like itemViews[options.index-1].$el.after( ... )), everything else should just work automatically^ assuming we keep the itemViews array in sync with the collection.

^ This probably won't work if you are doing anything fancy like overriding appendHtml

from backbone.marionette.

samccone avatar samccone commented on May 3, 2024

merged into v2.0 with #1161

from backbone.marionette.

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.