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

  • SEARCH
  • Home
  • 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 8879601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:59:06+00:00 2026-06-14T19:59:06+00:00

I’m having some problems with linking my models – which have their own views

  • 0

I’m having some problems with linking my models – which have their own views into a collection. I don’t know whether I am doing this the correct way or not. I don’t know if I need a view for the collection as well.

Here is the barebones code for my app.

var Model = Backbone.Model.extend ({
initialize : function () {
    new ModelView({model:this});
}
});

var ModelCollection = Backbone.Collection.extend({
initialize : function () {
    console.log('collected');
    this.on("add",function(){
        console.log('added model');
    });
},
model: Model
}); 

var Models = new ModelCollection;

var ModelView = Backbone.View.extend({
initialize : function () {
    console.log('view is loaded');
    this.render();
    this.model.on('change', this.render, this);
},
el: $('#menu'),
render : function () {
    var data = this.model.toJSON();
    var template = Handlebars.compile($("#menu-template").html());
    $(this.el).html(template(data));
    return this; 
},
});

var ModelCollectionView = Backbone.View.extend({
initialize : function () {
    console.log('Collection view created');
    Models.bind('add', this.addOne, this);
    Models.bind('reset', this.addAll, this);
    Models.bind('all', this.render, this);
},

addOne : function (model) {
    console.log('working');
    var view = new ModelView({model: model});
}
});

var ModelCollection = new ModelCollectionView;

I don’t know if i’m missing something here or whether I need this bit of code

var model = new Model();
Models.push(model);

I just can’t find a basic example of this anywhere. Thanks in advance.

  • 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-14T19:59:07+00:00Added an answer on June 14, 2026 at 7:59 pm

    Based on the code you show, I would suggest you go through a tutorial on backbone.js (Google is a good place to start). There is a lot of them out there, and it will help you understand the relations between views, models and collections.

    That being said, it seems strange to have a model which only creates a new view of the model.
    The whole point of a model is that is should contain data, which is again shown in the view.
    Look at how this example is distributing it:

    //Some doctors in an array, just mockupdata to create models from
    var someDoctors = [
        { name: "SomeName1" type: "Surgeon" },
        { name: "SomeName2" type: "Surgeon" },
        { name: "SomeName3" type: "Surgeon" },
        { name: "SomeName4" type: "Surgeon" }
    ];
    
    //define product model
    var Doctor = Backbone.Model.extend({
        defaults: {
            favoriteTool: "Stethoscope"
        }
    });
    
    //define a hospital collection of doctors
    var Hospital = Backbone.Collection.extend({
        model: Doctor
    });
    
    //define individual doctor view which renders a template based on data from the model
    var doctorView = Backbone.View.extend({
        tagName: "li",
        className: "doctor-container",
        template: $("#doctorTemplate").html(),
    
        render: function () {
            var tmpl = _.template(this.template);
            $(this.el).html(tmpl(this.model.toJSON()));
            return this;
        }
    });
    
    //define the hospital view
    var hosptialView = Backbone.View.extend({
        el: $("#doctors"),
    
        initialize: function () {
            this.collection = new Hosptial(someDoctors);
            this.render();
        },
    
        // go through all models in the hospital and calls renderDoctor for each model
        render: function () {
            var that = this;
            _.each(this.collection.models, function (item) {
                that.renderDoctor(item);
            }, this);
        },
    
        //create a view for the item (doctormodel) and appends it to this views el.
        renderDoctor: function (item) {
            var doctorView = new DoctorView({
                model: item
            });
            this.$el.append(doctorView.render().el);
        }
    });
    
    //create instance of hospital view
    var hosptial = new hosptialView();
    

    As you can see, the Collection is connected to doctor, the collection view creates a doctorview for each doctor and appends it to it self.

    If you want to listen for adds to the collection, do it in the collectionview initialize, and call renderDoctor:

    Hospital.bind('add', this.renderDoctor, this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.