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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:03:58+00:00 2026-06-09T05:03:58+00:00

When the view is initialized, how can I bind the model to the specific

  • 0

When the view is initialized, how can I bind the model to the specific View that is created? The view is current initialized at the start of the application. Also, how can I bind the model to the collection?
(function ($) { //loads at the dom everything

//Creation, Edit, Deletion, Date
var Note = Backbone.Model.extend({
    defaults: {
        text: "write here...",
        done: false
    },

    initialize: function (){
        if(!this.get("text")){
            this.set({"text": this.default.text});
        }
    },

    edit: function (){
        this.save({done: !this.get("done")});
    },

    clear: function (){
        this.destroy();
    }
});

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

var NoteView = Backbone.View.extend ({
    el: "body",

    initialize: function(){
        alert("initialized");
        var list = new NoteList;    
        return list;
    },

    events: {
        "click #lol" : "createNote"
    },

    createNote : function(){
        var note = new Note;
        this.push(note);
        alert("noted");
    }
});

var ninja = new NoteView;

})(jQuery);

  • 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-09T05:04:00+00:00Added an answer on June 9, 2026 at 5:04 am

    Update

    I just took a look at @James Woodruff’s answer, and that prompted me to take another look at your code. I didn’t look closely enough the first time, but I’m still not sure what you’re asking. If you’re asking how to have a model or view listen for and handle events triggered on the other, then check out James’s example of calling bind() to have the view listen for change (or change:attr) events on the model (although I’d recommend using on() instead of bind(), depending what version of Backbone you’re using).

    But based on looking at your code again, I’ve revised my answer, because I see some things you’re trying to do in ways that don’t make sense, so maybe that’s what you’re asking about.

    New Answer

    Here’s the code from your question, with comments added by me:

    var NoteView = Backbone.View.extend ({
    
        // JMM: This doesn't make sense. You wouldn't normally pass `el`
        // to extend(). I think what you really mean here is
        // passing el : $( "body" )[0] to your constructor when you
        // instantiate the view, as there can only be one BODY element.
    
        el: "body",
    
        initialize: function(){
            alert("initialized");
    
            // JMM: the next 2 lines of code won't accomplish anything.
            // Your NoteList object will just disappear into thin air.
            // Probably what you want is one of the following:
            // this.collection = new NoteList;
            // this.list = new NoteList;
            // this.options.list = new NoteList;
    
            var list = new NoteList;    
    
            // Returning something from initialize() won't normally
            // have any effect.        
    
            return list;
        },
    
        events: {
            "click #lol" : "createNote"
        },
    
        createNote : function(){
            var note = new Note;
    
            // JMM: the way you have your code setup, `this` will be
            // your view object when createNote() is called. Depending
            // what variable you store the NoteList object in (see above),
            // you want something here like:
            // this.collection.push( note ).
    
            this.push(note);
            alert("noted");
        }
    });
    

    Here is a revised version of your code incorporating changes to the things I commented on:

    var NoteView = Backbone.View.extend( {
    
      initialize : function () {
    
        this.collection = new NoteList;    
    
      },
      // initialize
    
    
      events : {
    
        "click #lol" : "createNote"
    
      },
      // events
    
    
      createNote : function () {
    
        this.collection.push( new Note );
    
        // Or, because you've set the `model` property of your
        // collection class, you can just pass in attrs.
    
        this.collection.push( {} );
    
      }
      // createNote
    
    } );
    
    
    var note = new NoteView( { el : $( "body" )[0] } );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I bind a backbone view to a collection rather than a model?
I've created a custom view controller that is initialized using a NIB. In the
I am using a uitoolbar in a view that is initialized using initWithFrame: method
How can I know which attribute of the view model is changed in the
While using this code, i can't get any results... Ext.define('Teste.view.Main', { extend: 'Ext.Container', initialize:
The View.remove() function in backbone js removes the container element of the view itself
Can't figure out what's wrong. When I click on a model title, it fetches
I use custom model binder to bind Order/OrderItem in my actions. This model binder
I'm trying to make a quick sample Backbone.js app that has a Post model,
I have a backbone collection and when I remove a model from the collection,

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.