You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following up on #2261, I see a very common use case being: change your filter, and render with that new filter. To that end, I am proposing a setFilter/removeFilter pair. They would behave as follows:
To change the filter and re-render the collection view, call setFilter with the new filter function. setFilter will not cause the view to render if
the view has not yet been rendered
the view has been destroyed
the new filter is the same as the existing filter
{ preventRender: true } is passed in the options
To remove the filter, use removeFilter, which accepts the same options
as setFilter and will cause a re-render under the same conditions.
varcv=newMarionette.CollectionView({childView: SomeChildView,emptyView: SomeEmptyView,collection: newBackbone.Collection([{value: 1},{value: 2},{value: 3},{value: 4}]),// Only show views with even valuesfilter: function(child,index,collection){returnchild.get('value')%2===0;}});// renders the views with values '2' and '4'cv.render();// removes the filter, re-renders and shows all viewscv.removeFilter();// re-renders the collection view, showing the views with values '1' and '3'cv.setFilter(function(child,index,collection){returnchild.get('value')%2!==0;});// removes the filter, but does not re-rendercv.removeFilter({preventRender: false});// shows all views, using the new filtercv.render();// re-renders the collection view, showing the empty viewcv.setFilter(function(child,index,collection){returnfalse;});// updates the filter, but does not re-rendercv.setFilter(function(child,index,collection){returntrue;},{preventRender: true});// shows all views, using the new filtercv.render();
So why use setFilter instead of manually calling render? I see this as a classic case of wanting to do something whenever you change a value, which is the exact use case of a setter. It's also easily overridable in case the users don't want this behavior (and it's entirely opt-in and thus BC).
I'm also explicitly avoiding Object.defineProperty setters to maintain compatibility with IE8 and because that could result in difficult-to-customize behavior. It might not be modern semantic JS, but I think it's the best way to handle this behavior in the present.
The text was updated successfully, but these errors were encountered:
Following up on #2261, I see a very common use case being: change your filter, and render with that new filter. To that end, I am proposing a
setFilter
/removeFilter
pair. They would behave as follows:To change the
filter
and re-render the collection view, callsetFilter
with the new filter function.setFilter
will not cause the view to render iffilter
is the same as the existingfilter
{ preventRender: true }
is passed in theoptions
To remove the filter, use
removeFilter
, which accepts the same optionsas
setFilter
and will cause a re-render under the same conditions.So why use
setFilter
instead of manually callingrender
? I see this as a classic case of wanting to do something whenever you change a value, which is the exact use case of a setter. It's also easily overridable in case the users don't want this behavior (and it's entirely opt-in and thus BC).I'm also explicitly avoiding
Object.defineProperty
setters to maintain compatibility with IE8 and because that could result in difficult-to-customize behavior. It might not be modern semantic JS, but I think it's the best way to handle this behavior in the present.The text was updated successfully, but these errors were encountered: