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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:20:33+00:00 2026-06-10T19:20:33+00:00

I’m new at Backbone.js.And I hava some problem at this keyworks.I hava a Backbone

  • 0

I’m new at Backbone.js.And I hava some problem at this keyworks.I hava a Backbone view blow:

 var print = Backbone.View.extend({
    el : $('ul li.newItem'),  
    events : { 'click li.newItem':'printText'},
    initialize:function(){ 
      _.bind(printText,this);  // does 'this' refer to the li.newItem ?
      alert(1233); // init does't work.
    },
    printText : function(){
      //I wanna print the hello world text each list item when clicked.
    }
  });

 var print = new print();  

Here is my demo : http://jsbin.com/evoqef/3/edit

  • 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-10T19:20:35+00:00Added an answer on June 10, 2026 at 7:20 pm

    You have two problems that are keeping your initialize from working:

    1. There is no printText in scope.
    2. _.bind and _.bindAll behave differently.

    The first is easy to fix, you want to use this.printText, not just printText.

    _.bind binds a single function to a this and returns that bound function; _.bindAll, on the other hand, binds several named functions to a this and leaves the bound functions attached to the specified this. So, doing this:

    _.bind(printText, this);
    

    doesn’t do anything useful as you’re throwing away the bound function. You’d want to do this:

    this.printText = _.bind(this.printText, this);
    

    Or more commonly in Backbone apps, you’d use _.bindAll:

    _.bindAll(this, 'printText');
    

    Now you have a functioning initialize and you’ll have the right this inside printText and we can move on to fixing printText. I think you want to extract the text from the <li> that was clicked; you can do this like this:

    printText: function(ev) {
        console.log($(ev.target).text());
    }
    

    But that still doesn’t work and we’re left to wondering what’s going on here. Well, Backbone binds events to a view’s el so let us have a look at that:

    var print = Backbone.View.extend({
        el : $('ul li.newItem'),
        //...
    

    When that Backbone.View.extend runs, there won’t be any li.newItem elements in the DOM so you won’t get a useful el in that view. The usual approach here would be to have a view that looks like this:

    var Print = Backbone.View.extend({
        tagName: 'li',
        events: {
            'click': 'printText'
        },
        render: function() {
            this.$el.text('Hello world ' + this.options.i);
            return this;
        },
        printText: function(e){
            console.log($(e.target).text());
        }
    });
    

    We set tagName to 'li' and let Backbone create the <li> by itself. Then we’d pass the counter value to the Print view as an argument, Backbone will take care of leaving the argument in this.options.i when we say new Print({ i: ... }).

    Now we just have to adjust the addItem method in your ListView to create new Prints and add them to the <ul>:

    addItem: function(){
        this.counter++;
        var v = new Print({ i: this.counter });
        this.$('ul').append(v.render().el);
    }
    

    Updated demo: http://jsbin.com/evoqef/10/edit

    I’ve also made a few other changes:

    1. Use this.$el instead of $(this.el), there’s no need to create something that’s already available.
    2. Use this.$() instead of $('ul', this.el), same result but this.$() doesn’t hide the context at the end of the $() function call and this.$() is more idiomatic in Backbone.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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 want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.