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 8996235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:43:38+00:00 2026-06-15T23:43:38+00:00

I am(as a beginner) making a small backbone function to append my links, for

  • 0

I am(as a beginner) making a small backbone function to append my links, for this i using the collection to be assigned model,

But the collection throw the error.. any one can correct my code please?

$(function(){
    var Model = new Backbone.Model({
        data:[
                {name:'Yahoo',href:'http://www.yahoo.com'},
                {name:'Gmail',href:'http://www.gmail.com'},
                {name:'Bing',href:'http://www.bing.com'}
              ]    
    });

    var Collection = new Backbone.Collection.extend({
        model:Model // is this not correct way to do?
    });

    var View = Backbone.View.extend({
        el:'#container',
        events:{
            'click button':'render'
            },
        initialize:function(){
            this.collection = new Collection(); //throw the error!
            this.template = $('#temp').children();
        },
        render:function(){
            var data = this.collection.get('data');
            for(i=0;i<data.length;i++){
                var li = this.template.clone().find('a')
                .attr('href',data[i].href)
                .text(data[i].name).end();
                this.$el.find('ul').append(li);
            }
        }
    });

    var myLink = new 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-15T23:43:39+00:00Added an answer on June 15, 2026 at 11:43 pm

    Models are used to store and manage individual chunks of data. In your case that would be a single name/href pair. Usually you use Backbone.Model.extend to create a model “class” with all your model-specific methods and properties and then you create instances of that model with new or set that model “class” as a collection’s model property so that the collection can create new instances of the model.

    Your model should look more like this:

    var Model = Backbone.Model.extend({
        defaults: {
            name: '',
            href: ''
        }
    });
    

    Notice that there’s no new in there as we’re just making a “class” to base our model instances on. Then we hook the model up to a collection (again using extend with no new):

    var Collection = Backbone.Collection.extend({
        model: Model
    });
    

    Now you can create an instance of the collection and hand it an array of data for the models:

    var links = new Collection([
        { name: 'Yahoo', href: 'http://www.yahoo.com' },
        { name: 'Gmail', href: 'http://www.gmail.com' },
        { name: 'Bing',  href: 'http://www.bing.com'  }
    ]);
    

    You’ll often pass the collection to the view rather than making the view instantiate the collection; Backbone will automatically set this.collection if you say new SomeView({ collection: some_collection }) so you can:

    var View = Backbone.View.extend({
        //...
        initialize: function() {
            this.template = $('#temp').children();
        },
    

    and say new View({ collection: links }) and access the collection through this.collection elsewhere in the view.

    A collection is a collection of several models (sort of like an array) and it will have various useful Underscore methods mixed in so you can iterate through the collection like this:

    render: function() {
        this.collection.each(function(link) {
            var li = this.template.clone().find('a').attr('href', link.get('href')).text(link.get('name'));
            this.$('ul').append(li);
        }, this);
    }
    

    Also notice the use of get to access model attributes, model attributes and object properties are different things. You can also use toJSON to flatten all the data at once. The final this argument to each makes this inside the callback the view. Backbone already supplies a this.$el.find() alias in the view’s this.$() method so I switch to that as well.

    Here’s a simplified demo: http://jsfiddle.net/ambiguous/WSqLM/

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

Sidebar

Related Questions

I am a beginner in C# , and making a calculator . But i
I am designing a small website using HTML + JSP. This is my first
i am a beginner in making website, i learned things at msdn only, but
first of all sorry im a beginner. Im making a small shoutbox with jquery
This is a rather broad question, but I'm a beginner-intermediate programmer that has just
I'm probably making a stupid beginner mistake here but SQL isn't one of my
I'm a beginner to SQL making a stored procedure with the goal of taking
Beginner iOS dev here. I have a problem in my array. Im making an
I am a beginner. I am making an education app which will read out
Can anybody point me to a good beginner's guide for making Facebook apps?

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.