Javascript 如何获取主干中的事件类型?

Javascript 如何获取主干中的事件类型?,javascript,model-view-controller,backbone.js,Javascript,Model View Controller,Backbone.js,收集和查看背部骨骼 var studentmodel = Backbone.Model.extend(); var studentcollection = Backbone.Collection.extend({ model : studentmodel, url : 'http://localhost/bb/data.JSON', parse : function(response){ //console.log(response.data); return resp

收集和查看背部骨骼

var studentmodel = Backbone.Model.extend();

    var studentcollection = Backbone.Collection.extend({
model : studentmodel,
url : 'http://localhost/bb/data.JSON',
parse : function(response){
    //console.log(response.data);
    return response.data;
    //return response;
},
  });



var studentlistview = Backbone.View.extend({
    tagName : "ul",
    className : "studentlistul",
    initialize : function(){
        this.model.bind("reset", this.checkEvent,this);
        this.model.bind("add", this.checkEvent,this);
    },
      checkEvent : function(){

        $(this.el).html("");

    _.each(this.model.models, function(student){

    $(this.el).append(new studentListItemView({ model : student}).render2().el);
    }, this);

    $('#studentlistdiv').html($(this.el));

    return this;
} });


尝试向这个模型及其工作添加项目,我的问题是在render fn中,当this.model.bind(“add”,this.checkEvent,this)这个evt启动时,如何获取事件类型。inside checkEvent如何获取事件的类型,即触发的是添加还是重置。这是我的问题,请帮助我

对于主干事件处理程序来说,没有可靠的方法知道是什么事件触发了它。而不是这样做:

this.model.bind("reset", this.checkEvent, this);
this.model.bind("add",   this.checkEvent, this);
对于每个所需的操作,您应该有单独的处理程序:

// A reset generally means that you want to redraw the whole thing.
this.model.bind("reset", this.render, this);

// If one model is added then you usually just want to render the
// the new one.
this.model.bind("add", this.render_one, this);
然后,您的
渲染
看起来有点像这样:

this.$el.empty();
this.collection.each(this.render_one, this);
return this;
当我在这里的时候,一些额外的东西:

  • 主干视图中已有其
    this.el
    的jQuery版本,因此无需
    $(this.el)
    ,只需使用
    this.$el
  • 主干视图的处理方式与它们处理模型的方式相同。因此,如果您
    new视图({collection:c})
    ,那么视图将自动拥有
    this.collection
    。如果您有一个集合,请使用
    集合
    ;如果您有一个模型,请使用
    模型
    ;使用准确的名称比较容易混淆
  • 主干集合有一组,因此您可以说
    this.collection.each(…)
    ,而不是
    (this.collection.models,…)