backbone.js 查看事件未触发

发布于 2024-12-26 11:16:51 字数 1348 浏览 0 评论 0原文

我是一个backbone.js n00b,所以请耐心等待。

我试图使用backbone.js 动态生成联系人列表,并为每个联系人附加一个 onclick 事件处理程序,但无济于事。我的视图渲染良好,但单击事件未注册。这是我的代码:

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:这是相应集合 UPDATE #2 的代码

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');
  }

});

:集合初始化和填充

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

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

UPDATE #3:模型定义

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: ''
  }
});

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: ''
  }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2025-01-02 11:16:51

您需要使用 View.el 属性才能使事件正常工作。

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;
    }

});

对模型代码的一个小改进:
(请注意,我删除了视图创建,这很重要!)

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: ''
    }
});

并开始:

App.Views.roster = App.Views.Contacts;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文