如何清除骨干网中点击路由器上javascript全局变量的值

时间:2014-01-09 07:14:31

标签: javascript backbone.js

我有一个javascript函数:

function Letter(){
   if(typeof Letter.instance === 'object'){
       return Letter.instance;
   }
   Letter.instance = this;
   var self = this;

   this.getList = function(id, key, startdate, enddate){
       console.log(Letter.qList);

       var url = getURL() +'?id=' + id + '&key='+ key + '&start=' + +'&end=' + enddate;
       Letter.qList = ajaxRequest(url,"GET","json","application/json",0);

       console.log(Letter.qList);
   };
}
Letter.qList = null;

qListLetter的全局变量。

这是骨干观点:

var letterHistory = new Letter();
var CustomerView = Backbone.View.extend({
    initialize: function() {

    },
    events : {
        'click #search' : 'searchLetterHistory'
    },
    'searchLetterHistory' : function(){
        letterHistory.getList('106','891177-0002','2012-08-07T13:58:19.403','2013-08-17T14:47:35.15');
    },
    render: function(){
        letterHistory.getList('106','','2012-08-07T13:58:19.403','2013-08-17T14:47:35.15'); 
        this.$el.append("<button id='search'>Search history</button>");
    }
});
return CustomerView;

当我转到render()

时,这是视图#customer的结果

enter image description here

点击按钮后:

enter image description here

问题:当我转到#home路由器并返回#customer时,我想结果将显示为第一张图片(qList设置为null)但它没有不像我想的那样,qList仍然保留着它的价值。

那么每当我路由到另一台路由器时,我怎么能清除全局变量(qList)。

2 个答案:

答案 0 :(得分:0)

设置Letter.qList = null; qList是Letter

的“静态”变量
  if(typeof Letter.instance === 'object'){
   Letter.qList = null;
   return Letter.instance;
}

或者你可以调用Letter.qList = null;你代码中的每一个地方

答案 1 :(得分:0)

尝试在返回现有对象时删除qList属性:

function Letter(){
  if(typeof Letter.instance === 'object'){
      delete Letter.qList; // *** notice this line ***
      return Letter.instance;
  }
  Letter.instance = this;
  var self = this;

  this.getList = function(id, key, startdate, enddate){
      console.log(Letter.qList);

      var url = getURL() +'?id=' + id + '&key='+ key + '&start=' + +'&end=' + enddate;
      Letter.qList = ajaxRequest(url,"GET","json","application/json",0);

      console.log(Letter.qList);
  };

}