Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:31:28+00:00 2026-06-17T21:31:28+00:00

I have a Backbone model (let’s call it Foo ) that includes a collection

  • 0

I have a Backbone model (let’s call it Foo) that includes a collection of n sub-models (let’s call them Bar), and in one particular view, I only want to display m of those sub-models, along with a message along the lines of “(n-m) remaining”.

Right now, what I’ve got is something like this:

var FooView = Backbone.View.extend({
    ...
    render: function() {
        this._barViews = [];
        var bars = this.model.get("bars");

        var that = this;
        _.each(bars.first(maxToShow), function(bar) {
            that._barViews.push(new BarView({model:bar}));
        }

        var remaining = bars.length - maxToShow;
        this.model.set("remaining", remaining > 0 ? remaining : undefined;

        var json = this.model.toJSON();
        $(this.el).html(this.template(json));

        _(this._holdViews).each(function(hv) {
            holdList.append($(hv.render().el));
        });
    }
});

This works, but it feels hacky, because I’m injecting “remainingMessage” into the model even though that’s specific to this particular view. (Another view might show all the bars, or none of them, and might or might not have a remaining message.) I’m also not totally excited about the nested views, since they mean creating an extra template file and having to remember to include it (FWIW, I’m using Handlebars.js for the templates, with server-side compilation).

Is there a better way to (1) filter the bars collection down to maxShown items, and (2) generate / include the number remaining in the view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:31:29+00:00Added an answer on June 17, 2026 at 9:31 pm

    You want a “view model” – a model that is there specifically to handle the concerns about the specific view that will use it. And fortunately, this is dirt simple in JavaScript.

    Using Object.create you can get a new object instance that inherits from the original object you pass in as a parameter. This gives us the ability to “decorate” the original model with new code, without actually changing the original model.

    In your case, we want to decorate the “foo” model with the remaining info. We only need that info in the toJSON results, though, so we’ll only add it to that method.

    
    
    function buildFooViewModel(model){
      var foovm = Object.create(model);
    
      foovm.toJSON = function(){
        // call the original model's toJSON
        var args = Array.prototype.slice.apply(arguments);
        var json = model.toJSON.apply(this, args);
    
        // add the needed "remaining" data using your calculations, here
        json.remaining = bars.length - maxToShow;
    
        // send the json data back
        return json;
      }
    
    }
    
    var FooView = Backbone.View.extend({
    
      initialize: function(){
        // use the view model instead of the original
        this.model = buildFooViewModel(this.model);
      },
    
      render: function(){
        // your normal render stuff here... calling this.model.toJSON
        // will return your JSON data with the `remaining` field in it already
      }
    
    });
    
    

    I do this quite often with my views that need calculations like this. You can see it happening all over http://ravenhq.com for example, in the database management screen, for % used / remaining, and other values like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a Backbone.Collection full of models; let's say that model is Car .
I have a Backbone collection model (with sub-models as elements) and views to edit
I have a Backbone View that has a Collection as its Model. If the
let's say I have : var Book = Backbone.Model.extend(); var Collection = Backbone.Collection.extend({ model:
I have a collection of Backbone models like so: window.Message = Backbone.Model.extend({}); window.MessageCollect =
In my Backbone application I have a model consisting of a couple of sub-models
I have a Backbone collection jQuery -> class App.Collections.List extends Backbone.Collection model: App.Models.ListItem I
Let's say, I have a backbone.js model named Item that is stored in an
Assume that I have a backbone model that has a bunch of boolean attributes:
I have two backbone models, loaded from server: var Model = Backbone.Model.extend({}); var SubModel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.