2014-09-22 61 views
1

原谅我的骨干我不是专家,必须执行功能显示绝对只有经过ResetQuestions自定义函数可能后的主干触发事件?

 ResetQuestions:function() { 
     //Great Code Here 
     }), 

我尝试这样做:

initialize: function() { 
    this.on("Show", this.Show, this); 
     }, 
    ResetQuestions:function() { 
     //Great Code Here 
      this.trigger("Show"); 
     }), 

但是,这是不成功的,没有任何人知道我可以做到这一点?

+2

的console.log这个值触发之前和initalize和检查都是相同 – StateLess 2014-09-22 10:09:15

+0

节目是否是一个函数?你想在ResetQuestions函数被调用时触发它?这是你的要求吗? – 2014-09-22 10:09:43

+0

是**显示**是一个功能,我可以把它命名为更好,我想在** ResetQuestions **函数后触发** Show ** @Raj – FrozenMonkeyLizard 2014-09-22 10:40:28

回答

1

不需要的事件,你可以简单地从其他函数调用函数

var sampleView = Backbone.View.extend({ 
     initialize: function() { 
      this.ResetQuestions(); 
     }, 
     Show: function() { 
      alert('i am at show'); 
     }, 
     ResetQuestions: function() { 
      // Execute all you code atlast call Show function as below. 
      this.Show(); 
     } 

    }); 
var view = new sampleView(); 
0
var sampleView = Backbone.View.extend({ 
    initialize: function(){ 
     this.ResetQuestions().promise().done(function() { // Using promise here. 
     this.Show(); 
     }); 
    }, 
    Show: function(){ 

    }, 
    ResetQuestions: function(){ 
    // Execute all you code atlast call Show function as below. 

    } 

}); 

然后开始你的观点,

var view = new sampleView(); 

希望这个作品!

+1

我试过了代码,它的行为相同,我需要一种方法来逃避异步,问题是,我需要执行**显示**绝对,只有** ResetQuestions **完成后 – FrozenMonkeyLizard 2014-09-22 11:12:40

+1

因此,使用promise api,这是在构建在jQuery中。 – 2014-09-22 12:16:34

0

也许你只是困惑什么运行什么,并命名事件和方法相同的名称Show。我用你的代码创建了一个jsfiddle - http://jsfiddle.net/yuraji/aqymbeyy/ - 你调用ResetQuestion方法,它触发Show事件,并且Show事件运行Show方法。

编辑:我已经更新了小提琴演示,你可能必须绑定方法的实例,我用_.bindAll。如果你不这样做,你可能会以事件为背景(this)。

编辑︰然后,如果您的ResetQuestions运行异步代码,如ajax请求获得新的问题,您将不得不确保您的Show事件在请求完成时触发。

相关问题