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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:05:49+00:00 2026-06-09T09:05:49+00:00

I have the following in test.html: <script> var Foo = Backbone.Model.extend({ initialize: function(options) {

  • 0

I have the following in test.html:

<script>
var Foo = Backbone.Model.extend({
  initialize: function(options) {
    console.log('hello!');
  }
});
var Bar = Backbone.Collection.extend({
  model: Foo
});
var m = new Bar({});
</script>

As it turns out, when the variable m is initialized, the initialize function of Foo is called. Thus, in the Firebug console, I get ‘hello!’. When I comment out the line:

model: Foo,

There is no ‘hello!’ in the console output. Thus, declaring a model for a collection calls that model’s initialize function. I think this behavior is a bit silly. I haven’t read through backbones code, but is there a reason for this?

  • 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-09T09:05:50+00:00Added an answer on June 9, 2026 at 9:05 am

    Well, there’s nothing wrong with the behaviour of the code.
    When you pass the model in the collection definition, you specify that every model in that collection will be of type Foo.
    When you initialize the collection new Bar({}), you pass a model to the collection (although, as @alexanderb stated, I think that the collections expects an array as a first argument) and it initializes it, thus outputting 'hello!'.
    For example, if you do not pass any models to the collection constructor :

    new Bar();// no console output
    

    there will be no console output, and on the other hand, if you would pass an array of objects, then the collection would initialize all of the provided models :

    new Bar([{},[},{}]);// will output 'hello!' 3 times
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following page: <html> <head> <title>Test</title> <script type=text/javascript> function BuildTree() { var
I have the following HTML / JS: <html> <body> <script> function foo(){ var html
I have the following script inside a HTML page: <script> function Test(){ alert(i got
I have the following in a html.erb file: <%= @location_list = [['test',2]] %> <script
I have the following code: searchSt = Test, searchEd = in; var html =
I have the following jQuery script... $(function() { $('.eventWrapper').bind('click', function(event) { var srvName =
i have the following code : <html> <head><title></title> <script> function showMe (it, box) {
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
I have following test.pyx cdef public class Foo[object Foo, type FooType]: cdef public char*
So I have the following code: <script type=text/javascript> $(document).ready( function () { $(#txt).click(function ()

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.