javascript 引发 Backbone.js View 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9350726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:21:37  来源:igfitidea点击:

Raising a Backbone.js View event

javascriptbackbone.js

提问by James

I'd like to find a way to raise a backbone.js "event" without something having changed in the model or in the dom.

我想找到一种方法来引发一个backbone.js“事件”,而模型或dom中没有任何改变。

For instance, I'm loading the Facebook SDK asynchronously. I'm subscribed to the auth.login event, and would like to send a message to my view that the user has logged in so it can re-render itself appropriately.

例如,我正在异步加载 Facebook SDK。我订阅了 auth.login 事件,并想向我的视图发送一条消息,表明用户已登录,以便它可以适当地重新呈现自身。

My view looks similar to this:

我的观点与此类似:

window.CreateItemView = Backbone.View.extend({
  el: $('#content'),
  initialize: function() {
    this.render();
  },
  render: function() {
    // do something
    return this;
  },
  setSignedRequest: function(signedRequest) {
    //do something with signedRequest
  }
});

In my facebook code, I do this:

在我的 facebook 代码中,我这样做:

  FB.Event.subscribe('auth.login', function(response){
    if (response.status === 'connected') {
      var uid = response.authResponse.userID;
      var accessToken = response.authResponse.accessToken;
      window.signedRequest = response.authResponse.signedRequest;

      if (window.view && window.view.setSignedRequest) {
          window.view.setSignedRequest(window.signedRequest);
      }
    }
  });

However, while window.view exists, it cannot see the setSignedRequest method. I've ensured that my scripts are loading in the correct order. Strangely I have this same code on a different page albeit a different View object and it works fine. I haven't seen any difference that would account for this.

但是,虽然 window.view 存在,但它看不到 setSignedRequest 方法。我确保我的脚本以正确的顺序加载。奇怪的是,我在不同的页面上有相同的代码,尽管是不同的 View 对象,但它工作正常。我没有看到任何可以解释这一点的差异。

A better solution would be to raise some sort of event and have the view listen for it. However, I don't want to utilize the change event on the model as the signedRequest shouldn't be a property of the model. Is there a better way of accomplishing this?

更好的解决方案是引发某种事件并让视图监听它。但是,我不想在模型上使用更改事件,因为 signedRequest 不应该是模型的属性。有没有更好的方法来实现这一点?

回答by Tom Tu

Backbone.Viewis extended with Backbone.Eventswhich means you can easily trigger custom events and pass whatever data you want

Backbone.View扩展了Backbone.Events这意味着您可以轻松触发自定义事件并传递您想要的任何数据

var View = Backbone.View.extend({

    initialize: function() {

        this.on('customEvent', this.doSomething, this);
    }

    doSomething: function(someData) {

        // this!
    }
});

var view = new View();

view.trigger('customEvent', "someDataHere");

though I don't know why you can't see the method on the view - it should work - Are you 100% sure you are instantiating the view correctly? and that window.viewis instance of CreateItemView?

虽然我不知道为什么你看不到视图上的方法 - 它应该可以工作 - 你是否 100% 确定你正在正确实例化视图?那window.viewCreateItemView?

回答by ggozad

If you want to do that outside your model and subscribe to the event on your view you could do the following:

如果您想在模型之外执行此操作并订阅视图上的事件,您可以执行以下操作:

var SomeObject = {...};
_.extend(SomeObject, Backbone.Events);

Then, you can subscribe to events on SomeObject

然后,您可以订阅 SomeObject 上的事件

SomeObject.on('myevent', someFunc, this);

or trigger the event

或触发事件

SomeObject.trigger('myevent', data);