2013-02-15 153 views
0

我的骨干函数我想触发一个条件方法,该函数已经在该对象内,在events声明中通过用户点击添加,这工作正常。但是,我的状态点击事件并不触发..单击事件不触发

这里是我的代码:

在视图部分

  events:{ 
         "click .mainMenu a":"listTrigger" // this is work on user click 
        }, 
     initialize:function(params){ 
        _.bindAll(this); 
        var that = this; 
        this.listItems = ["projectName","assignedTo","projectName"]; 
        this.classItems = ["projectName","assignedTo","sortBy"]; 
        this.listCatch = [];this.boardCatch=[]; 
        this.params = params, 
        this.filterState = false, 
        this.listTriggerElement = "";//i am initiating a element variable 

        for(var i=0;i<this.listItems.length; i+=1){ 
         this.listCatch[i] = []; 
        } 

        this.collection = new singleton.collection; 
     //   this.collection.on('reset', this.render); 
        this.collection.on('add', this.render); 
        this.collection.on('remove', this.render); 
        var dataFetcher = function(){ 
         that.collection.fetch({update:true,remove:true}); 
         appDataFetcher = setTimeout(dataFetcher,10000); 
        }; 
        var appDataFetcher = setTimeout(dataFetcher,0); 
       }, 
render:function(){ 
      this.listCollection  = this.collection; 
      this.boardCollection = this.collection; 
      this.filterCollection = this.collection; 
      this.listCollect(); 
this.filterState != true ? this.boardViewSet() : this.listTriggerElement.trigger('click'); 
// checking by condition and triggering click.. not working 
      }, 

    listTrigger:function(e){ //it should work by user click as well data udpate..  
       e.preventDefault(); 
       this.listTriggerElement = $(e.target); 
       var filterKey = $(e.target).text(); 
       var category = $(e.target).parents('ul').prop('class'); 

       var collect = _.filter(this.filterCollection.models, function(model){ 
        return model.get(category) === filterKey; 
       }); 

       var newColl = new singleton.collection(); 
       newColl.reset(collect); 

       this.boardCollection = newColl; 
       this.boardViewSet(); 
       this.filterState = true; 
      } 

我怎么能解决这个问题..?

回答

2

这个视图范围内是否包含“.mainMenu a”元素?

如果没有你需要的 “厄尔尼诺” 对象设置为你的父容器,如:

http://jsfiddle.net/C9wew/4347/

myView = Backbone.View.extend({ 
    el: "#container", 
    render: function(){ 
    }, 
    events: { 
     "click button": "doSomething" 
    }, 
    doSomething: function(event){ 
     console.log(345345); 
    } 
}); 

new myView(); 

而如果你没有定义 “范围”(el的)它不会工作。

+0

是的,类名上有一个范围。我需要获取类名并获取数据.. – 3gwebtrain 2013-02-15 11:08:12

+0

Stur是正确的,您需要指定el属性,className也不适用于我 – 2013-02-17 02:20:37