在事件中使用变量作为选择器

发布于 2024-12-19 13:36:14 字数 325 浏览 0 评论 0原文

由于某种原因,我需要使用变量作为主干中事件的选择器,但我不知道如何执行此操作:

app.views.Selfcare = Backbone.View.extend({
    events: {
        click window.parent.document .close : "closeWindow"
    },
    closeWindow: function() {
        //code
    }
});

我必须使用不同的范围,并且我无法执行“click .close”:“closeWindow” 。

谢谢你的帮助。

for some reason I need to use a variable as the selector for events in backbone, but I can't figure how to do this :

app.views.Selfcare = Backbone.View.extend({
    events: {
        click window.parent.document .close : "closeWindow"
    },
    closeWindow: function() {
        //code
    }
});

I have to use a different scope and I can't do "click .close" : "closeWindow".

Thx for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-12-26 13:36:14

我查看了 Backbone.js 的源代码,发现如果您的视图的 events 是一个函数,则调用该函数并将其返回值用作 events 对象。

这意味着您的代码可以这样更改:

app.views.Selfcare = Backbone.View.extend({
  events: function() {
    var _events = {
      // all "standard" events can be here if you like
    }
    _events["events" + "with variables"] = "closeWindow";
    return _events;

  },
  closeWindow: function() {
    //code
  }
});

THIS 是源代码中有趣的部分:

if (_.isFunction(events)) events = events.call(this);

更新:

示例可在 JSFiddle 这里**

I had a look at Backbone.js's source code and found out that if your view's events is a function then the function is called and it's return value is used as the events object.

This means that your code can be changed like this:

app.views.Selfcare = Backbone.View.extend({
  events: function() {
    var _events = {
      // all "standard" events can be here if you like
    }
    _events["events" + "with variables"] = "closeWindow";
    return _events;

  },
  closeWindow: function() {
    //code
  }
});

THIS is the interesting part of the source code:

if (_.isFunction(events)) events = events.call(this);

Update:

Example is available on JSFiddle HERE**

山川志 2024-12-26 13:36:14

我不确定您是否能够在那里使用变量。您可以使用内置事件方法(请参阅文档)添加自定义侦听器,然后将事件侦听器添加到 window.parent.document 以触发该自定义事件(使用 Events.trigger 方法)。

也就是说,将此事件与 Backbone 完全分离(除非您不想这样做),然后沿着 addEventListener 路线走下去会容易得多:

app.views.Selfcare = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render', 'closeWindow');
        if(this.options.clickTarget) {
            this.options.clickTarget.addEventListener('click', this.closeWindow, false);
        }
    },
    render: function() {
        // Render to the DOM here
        return this; // as per Backbone conventions
    },
    closeWindow: function() {
        // Stuff here
    }
});

// Usage:
var mySelfcare = new app.views.Selfcare({
    clickTarget: window.parent.document
});

我认为这应该可行,尽管我还没有测试过它(并且那里可能是一两个语法错误!)

I'm not sure that you'll be able to use a variable there. You could use the built-in Events methods (see documentation) to add a custom listener, then add an event listener to window.parent.document to trigger that custom event (use the Events.trigger method).

That said, it would be much easier to decouple this event from Backbone entirely (unless you don't want to do that), and go down the addEventListener route:

app.views.Selfcare = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render', 'closeWindow');
        if(this.options.clickTarget) {
            this.options.clickTarget.addEventListener('click', this.closeWindow, false);
        }
    },
    render: function() {
        // Render to the DOM here
        return this; // as per Backbone conventions
    },
    closeWindow: function() {
        // Stuff here
    }
});

// Usage:
var mySelfcare = new app.views.Selfcare({
    clickTarget: window.parent.document
});

I think that should work, although I haven't tested it (and there may be one or two syntactical errors!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文