Backbone JS 事件函数修改对象

发布于 2025-01-03 14:53:29 字数 446 浏览 4 评论 0原文

我正在使用 BackboneJS 开发一个相当复杂的应用程序。我有一个 Globals 对象,用于发布/订阅事件。挑战在于,似乎没有一种有效的方法可以使用事件驱动架构来解耦功能。

我更多地考虑像 Magento 这样的事件系统。您会看到,在 Magento 中,您调用 dispatchEvent,并传递一个对象。 Magento 中的下一行(这将是中断的,意味着它正在等待该事件的所有订阅者首先执行),您可以再次访问这些对象。这种方式的好处是,任何订阅事件的函数都会传递一个它可以修改的对象,然后返回该对象。

因此,当订阅者完成执行时,我们可以获取这个修改后的对象并完成执行。在 JS 中,这将是一个回调,但想法是相同的。

所以我的问题是:

  • 你知道 Backbone 中有类似的东西吗?
  • 有你知道的插件吗?

谢谢您的宝贵时间!

I am working on a fairly complex application with BackboneJS. I have a Globals object that I use to publish/subscribe to events. The challenge is that it doesn't seem like there is an efficient means to decouple the functions using event-driven architecture.

I am thinking of more of an eventing system like Magento. You see, in Magento, you call dispatchEvent, and pass along an object. The next line in Magento (which would be breaking, meaning, it's waiting for all subscribers to that event to execute first), you can access those objects again. The nice thing with this way is that any function that subscribes to the event is passed an object that it can modify, and then return that object.

So, when the subscribers have finished executing, we can take this modified object and finish execution. In JS, that would be a callback, but the idea is the same.

So my questions:

  • Are you aware of anything like this baked into Backbone?
  • Are there plugins that you know of?

Thank you for your time!

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

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

发布评论

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

评论(1

荒芜了季节 2025-01-10 14:53:29

您可以定义自己的事件并将任何参数传递给您想要的事件处理程序。例如,您可以定义一个事件并在触发该事件时传递一个对象:

var M = Backbone.Model.extend({
    go: function() {
        var o = { };
        this.trigger('some:event', o);
        console.log(o);
    }
});

然后侦听器可以将他们想要的任何内容存储在他们作为参数获得的对象中:

var m = new M;
m.on('some:event', function(o) {
    o.where_is = 'pancakes house?';
});
m.on('some:event', function(o) {
    o.ill_cook = 'you some eggs, Margie';
});
m.go();

这将为您留下 {where_is: 'pancakes house? ', ill_cook: 'you some Eggs, Margie'} in o 当您在 go 中调用 console.log 时。

演示: http://jsfiddle.net/ambigously/4XCmc/

标准 Backbone 事件有自己指定的当然,听众的参数列表,但您可以添加您需要的任何自定义事件。

You can define your own events and pass whatever parameters to the event handlers that you want. For example, you can define an event and pass an object when triggering that event:

var M = Backbone.Model.extend({
    go: function() {
        var o = { };
        this.trigger('some:event', o);
        console.log(o);
    }
});

and then the listeners can stash whatever they want in the object they get as an argument:

var m = new M;
m.on('some:event', function(o) {
    o.where_is = 'pancakes house?';
});
m.on('some:event', function(o) {
    o.ill_cook = 'you some eggs, Margie';
});
m.go();

That will leave you with {where_is: 'pancakes house?', ill_cook: 'you some eggs, Margie'} in o when you hit the console.log call in go.

Demo: http://jsfiddle.net/ambiguous/4XCmc/

The standard Backbone events have their own specified argument lists for the listeners of course but you can add whatever custom events you need.

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