点击“此"在骨干视野中 [英] Click on "this" in a backbone view

查看:75
本文介绍了点击“此"在骨干视野中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是骨干级的新手,事件系统有点问题.

I am a newbie with backbone and i have a little problem with it events system.

我有一个表示li元素的视图,我想在单击它时做点什么.

I have a view who represents an li element, and i want to do something when i click on it.

这是我的代码:

var IndicatorView = Backbone.View.extend({
  tagName: 'li',
  className: 'indicator',
  initialize: function(options){
    _.extend(this, _.pick(options, "controller"));
    this.model.on('change', this.render, this);
    var self=this ;
    this.$el.on("click", function(){
      alert(self.model.get('name'));
    })
  },
  render: function(){
    this.$el.html(this.model.get('name'));
    return this; // enable chained calls
  }
});

目前,它可以工作,但是使用jQuery事件.我如何对骨干事件做同样的事情?感谢您的回答:)

For the moment, it works, but using jQuery events. How can I do the same thing with backbone events ? Thanks for your answers :)

推荐答案

使用空选择器将事件绑定到视图el:

Use an empty selector to bind an event to the view el:

var IndicatorView = Backbone.View.extend({
    tagName: 'li',
    className: 'indicator',

    events: {
        'click': function() {
            alert(this.model.get('name'));
        }
    },

    initialize: function(options){
        _.extend(this, _.pick(options, "controller"));

        // listenTo is recommended over on
        // http://backbonejs.org/#Events-listenTo
        this.listenTo(this.model, 'change', this.render);
   },
   render: function(){
       this.$el.html(this.model.get('name'));
       return this; // enable chained calls
   }
});

有关更多信息,请参见 http://backbonejs.org/#View-delegateEvents

See http://backbonejs.org/#View-delegateEvents for more info

这篇关于点击“此"在骨干视野中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆