如何清除主干中click router上javascript全局变量的值
我有一个javascript函数:如何清除主干中click router上javascript全局变量的值,javascript,backbone.js,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.qL
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;
qList
是Letter
的全局变量
这是主干视图:
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;
var letterHistory=新字母();
var CustomerView=Backbone.View.extend({
初始化:函数(){
},
活动:{
“单击#搜索”:“searchLetterHistory”
},
“searchLetterHistory”:函数(){
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');
本.$el.append(“搜索历史”);
}
});
返回CustomerView;
这是路由到#customer
单击按钮后:
问题:当我转到#home
路由器,回到#customer
时,我想结果会显示为第一张图片(qList
设置为null),但它不像我想的那样,qList
仍保留着它的值
因此,当我路由到另一个路由器时,如何清除全局变量(qList)。设置Letter.qList=null;qList是一个“静态”变量
if(typeof Letter.instance === 'object'){
Letter.qList = null;
return Letter.instance;
}
或者您可以调用Letter.qList=null;返回现有对象时,请尝试从代码的每个位置删除
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);
};
}同样的问题也会出现。我相信这是行不通的。Domo尝试使Letter充当singleton,因此delete Letter.qList首先必须在内部,如果是这样的话,我应该如何处理它,才能像我的问题中那样删除它。if(typeof Letter.instance=='object'){delete Letter.qList;return Letter.instance;}sory它必须是Letter.qList=null;请看我的最新答案我试过你的最新答案Azzy Elvul,但不起作用。当我回到#家然后再回到#客户时,该对象仍然具有该值。如果我刷新页面,它将为null。但是我不想刷新它(这个问题发生了,因为我还没有用你的答案更新我的代码)?必须是call new Letter()才能清除qList,或者在某个地方必须调用Letter.qList=null;因为Letter.qList是“静态”的,所以它会保存它;s值,直到字母为“活动”对象。