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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:29:47+00:00 2026-05-27T15:29:47+00:00

I have the following HTML code: <ul id=’item-list’></ul> <button id=’add-item’>Add Item</button> <script type=’text/template’ id=’item-template’>

  • 0

I have the following HTML code:

<ul id='item-list'></ul>
<button id='add-item'>Add Item</button>
<script type='text/template' id='item-template'>
  <li><%= title %></li>
</script>

and the following javascript / backbone js code:

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


var ItemListView = Backbone.View.extend({
   el : $("#item-list"),
   initialize(options) {
     this.item_collection = new ItemCollection();
   }, 
   appendItem: function() {
     new_item = new Item({'title' : 'New Item'});
     this.item_collection.add(new_item);
     item_template = $("#item-template");
     item_html = _.template(item_template,new_item.toJSON());
     this.el.append(item_html);
   }
});

var AddItemView = Backbone.View.extend({
  el: $("add-item"),
  events: {
    "click" : "addItem"
  },
  addItem: function() {
    // ?
  }
});

item_list_view = new ListItemView();
add_item_view = new AddItemView();

How can I add a new item to the item list view and collection from an event in the addItemView View? also, should the creation of the model, appending it to the collection, and appending it to the view all take place in the ListItemView.addItem() function, or should I instead have it binded to the add event of the ItemCollection ? I am still having some trouble wrapping my head around the way bindings and the interactions between various views, models, and collections should work.

  • 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-05-27T15:29:48+00:00Added an answer on May 27, 2026 at 3:29 pm

    here’s an example of events between 2 views working with a model/collection. Basically, use collectionName.bind(‘add’,yourFunction,this);

        <script type="text/template" id="item-template">
          <div class="nonedit"><span ><%= name %> (<%= age %>)</span> <a class="delete" href="#">X</a>
          <div class="edit"><input /></div>
    
        </script>
        <body>
            <ul class="1"></ul>
            <div class="count">
                <div></div>
                <input id="name" placeholder="enter a name"/>
                <input id="age" placeholder="enter age"/>
            </div>
    
        </body>
    

    var Person = Backbone.Model.extend({
        defaults:function(){
            return {
                name:'unknown',
                age:0   
            };
        }
    });
    
    var People = Backbone.Collection.extend({
        model:Person
    });
    
    var people = new People;
    
    var Li = Backbone.View.extend({
        tag:'li',
        class:'name',
        template:_.template($('#item-template').html()),
        events:{
            'click a.delete':'remove'
        },
        initialize:function(){
            this.model.bind('change',this.render,this);
            $(this.el).html(this.template(this.model.attributes));
        },
        render:function(){
            $(this.el).html(this.template(this.model.attributes));  
        },
        remove:function(){
            this.model.destroy();
            $(this.el).fadeOut(300);
            setTimeout(function(){$(this.el).remove()},400);
        },
        modify:function(){
            $(this.el).addClass('edit');
        }
    });
    
    var Ul = Backbone.View.extend({
        el:$('ul'),
        events:{
    
        },
        initialize:function(){
            people.bind('add',this.add,this);
        },
        render:function(){
    
        },
        add:function(model){
            var li = new Li({model:model});
            this.el.append(li.el);
        }
    
    
    });
    
    var ul = new Ul;
    
    
    
    var Div = Backbone.View.extend({
        el:$('.count'),
        nameInput:$('#name'),
        ageInput:$('#age'),
        events:{
            'keypress input#name':'keypress',
            'keypress input#age':'keypress',
        },
        initialize:function(){
            people.bind('add',this.add,this);
        },
        render:function(){
    
        },
        add:function(e){
            this.el.find('div').html(people.length);
        },
        keypress:function(event){
            if(event.which == 13){
                people.add({name:this.nameInput.val(),age:this.ageInput.val()});
            }
        }
    });
    
    var div = new Div;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following html code <input type=button id =b1 value=Click me onclick=msg() /> <input
I have the following html code: <input type=text id=theInput value=/> <a href=# id=theLink>Click me</a>
I have the following HTML code <div class=text>bla bla bla bla</div> <div class=button>Show</div> And
I have the following HTML code: <td> <input type=text size=40 value= name=related_image id=related_image> <input
I have the following html code: <h3 id=headerid><span onclick=expandCollapse('headerid')>&uArr;</span>Header title</h3> I would like to
I have the following html code: <mytag> Just Some Text </mytag> And I have
I have the following HTML Code <%@ Page Language=C# %> <html> <head> <title></title> </head>
i have the following html code : <FORM name=frmmail> <input id=dochtmlContent type=hidden name=dochtmlContent value=oldValue/>
I have the following html code: <div class=Radios> <span class=radio> <input type=radio value=1> yes
I have the following HTML code / structure: <div id=container> <div id=div1>text</div> <div id=div2>more

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.