2

I am sort of a backbone.js n00b so please bear with me.

I am trying to dynamically generate a list of contacts using backbone.js, and attach an onclick event handler to each, to no avail. My views are rendering fine, but click event does not register. Here is my code:

App.Views.Contact = Backbone.View.extend({

initialize: function() {
  this.render();
},

events: {
  'click': 'select',
},

select: function(event) {
  console.log('contact selected');
},

render: function() {
  $('#contacts').append(tmplContact(this.model.toJSON()));
}

});

UPDATE: here is the code for the corresponding collection

App.Collections.Contact = Backbone.Collection.extend({

  initialize: function() {
    this.bind('reset', function() {
      console.log('COLLECTION RESET');
    });
  },

  model: App.Models.Contact,

  comparator: function(model) {
    return model.get('name');
  }

});

UPDATE #2: collection initialization and population

App.Collections.roster = new App.Collections.Contact;

function getContacts() {
    socketRequest('friends', function(data) {App.Collections.roster.reset(data)});
}

UPDATE #3: model definition

App.Models.Contact = Backbone.Model.extend({
  initialize: function() {
    this.set({id: this.get('token')});
    this.set({avatar: parseAvatar(this.get('avatar'))});
    this.set({name: parseName(this.toJSON())});
    this.set({view: new App.Views.Contact({model: this})});
  },

  defaults: {
    username: ''
  }
});
1
  • 1
    are you using a collection too?
    – roberkules
    Jan 12, 2012 at 1:30

1 Answer 1

4

You need to use the View.el property in order to get the events working.

App.Collections.Contact = Backbone.Collection.extend({

    initialize: function() {
        this.bind('reset', function() {
            console.log('COLLECTION RESET');
        });
    },

    model: App.Models.Contact,

    comparator: function(model) {
        return model.get('name');
    },

    render: function() {
        ;
    }

});

App.Views.Contacts = Backbone.View.extend({

    el: '#contacts',

    initialize: function() {
        this.collection = new App.Collection.Contact;
        this.collection.bind('reset', this.render, this);

        var that = this;
        socketRequest('friends', function(data) {
            this.reset(data);
        });
    },

    render: function() {
        $(this.el).html('put your template here');
        this.collection.each(this.addOne, this);
        return this;
    },

    addOne: function(contact) {
        var view = new App.Views.Contact({model: contact});
        contact.view = view; // in case you need the ref on the model itself
        $(this.el).append(view.render().el);
    }

});

App.Views.Contact = Backbone.View.extend({

    el: '',

    tagName: 'div',

    events: {
        'click': 'select',
    },

    select: function(event) {
        console.log('contact selected');
    },

    render: function() {
        $(this.el).html(htmltmplContact(this.model.toJSON()));
        return this;
    }

});

a small improvement to your model code:
(notice that i removed the view creation, this is important!)

App.Models.Contact = Backbone.Model.extend({

    parse: function(c) {
        c.id = c.token;
        c.avatar = parseAvatar(c.avatar);
        c.name = parseName(c);
        return c;
    },

    defaults: {
        username: ''
    }
});

and to kick things off:

App.Views.roster = App.Views.Contacts;
5
  • The issue is that I have many more than one contact, and that I don't (and won't) have distinct IDs for each element, and thus can't figure out how to set an appropriate el property. Any ideas? Jan 12, 2012 at 1:34
  • 1
    thats easy to solve. just update your post with the code of the collection and I can provide you a fix.
    – roberkules
    Jan 12, 2012 at 1:36
  • 1
    sorry, one more thing that i need... the code of the collection-view (where you create the collection and iterate over the models, creating the contact views)
    – roberkules
    Jan 12, 2012 at 1:43
  • just updated my answer. added a new Contacts view, which is basically a view for the collection and added your socketRequest call there as well, should work :)
    – roberkules
    Jan 12, 2012 at 2:03
  • just saw that there was a bug in my code... App.Views.Contacts#addOne is fixed now
    – roberkules
    Jan 12, 2012 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.