EmberJs是否支持发布/订阅者事件模式? [英] Does EmberJs support publish/subscriber eventing pattern?

查看:85
本文介绍了EmberJs是否支持发布/订阅者事件模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在评估一个JavaScript框架,用于我们的下一个项目。我真的很喜欢Ember。但是在我们的应用程序中,我们需要一个来自数据层的事件机制来通知控制器层有些事情发生了变化。例如,我知道可以使用Ember中的obersever:

  person.addObserver('fullName',function(){
//处理变更
});

但是我更喜欢Backbone.Events,您可以订阅或发布活动,具体来说: p>

  var object = {}; 

_.extend(object,Backbone.Events);

object.on(alert,function(msg){
alert(Triggered+ msg);
});

object.trigger(alert,an event);任何人都知道这是否可以在EmberJS中使用?


$ b $ p

$ b

为了给我一些关于我的问题的背景,我们的应用程序是一个实时应用程序。因此,后端RESTful服务会不时地将一个事件触发到客户端(JavaScript端)。我想要一个封装对后端RESTful服务的访问的数据层,但也保留缓存。我希望EmberJS.Data可以帮助我(这是一个单独的问题,我想找到答案)。这样,当我们从后端RESTful服务发生变化时,我也希望更新缓存。一旦缓存对象被更新,我希望通知控制器层。这基本上是为什么我需要JavaScript方面的一些事件机制。



请注意,我不想使用观察者的原因是有时,一个事件可能意味着我必须执行一个动作,即加载一个消息,或者指示一个语音呼叫来了。



谢谢

解决方案

自从 2326580 - 自 v0.9.6 - 有一个 Ember.Evented Mixin ,请参见 http://jsfiddle.net / qJqzm /

  var handler = Ember.Object.create({
otherEvent:function(){
console.log(arguments);
}
});

var myObject = Ember.Object.create(Ember.Evented,{
init:function(){
this._super();
this.on 'alert',这个'alert');
},
alert:function(){
console.log(arguments);
}
});
myObject.on('otherEvent',handler,'otherEvent');

myObject.fire('alert',{
eventObject:true
});
myObject.fire('otherEvent',{
first:true
},{
second:true
});


I'm recently evaluating a JavaScript framework to be used for our next project. I really like Ember. But in our application, we would need an eventing mechanism from the data layer to notify the controller layer that something has changed. I know one could use obersevers in Ember, for example,:

person.addObserver('fullName', function() {
  // deal with the change
});

But I like more in Backbone.Events that you could subscribe or publish an event, specifically:

var object = {};

_.extend(object, Backbone.Events);

object.on("alert", function(msg) {
  alert("Triggered " + msg);
});

object.trigger("alert", "an event");

Anybody has an idea whether that's doable in EmberJS?

To also give you some background about my question, our application is a real-time application. So from time to time, the backend RESTful service will fire an event to the client side (JavaScript side). I would like to have a data layer which encapsulate the access to the backend RESTful service, but also keeps a cache. I hope EmberJS.Data could help me with that (that's a separate question I want to find answers). With that, I would like also the cache to be updated whenever a change happened from the backend RESTful service. Once the cache object is updated, I would like the Controller layer to be notified. This is basically why I need some eventing mechanism in JavaScript side.

Note that the reason I don't want to use the Observers is that sometimes, an event could mean I have to perform an action, i.e. load up a message, or indicate that a voice call is coming. The backbone way seems more natuarl to me.

Thanks

解决方案

Since commit 2326580 - which is available since v0.9.6 - there is an Ember.Evented Mixin, see http://jsfiddle.net/qJqzm/.

var handler = Ember.Object.create({
    otherEvent: function() {
        console.log(arguments);
    }
});

var myObject = Ember.Object.create(Ember.Evented, {
    init: function() {
        this._super();
        this.on('alert', this, 'alert');
    },
    alert: function() {
        console.log(arguments);
    }
});
myObject.on('otherEvent', handler, 'otherEvent');

myObject.fire('alert', {
    eventObject: true
});
myObject.fire('otherEvent', {
    first: true
}, {
    second: true
});​

这篇关于EmberJs是否支持发布/订阅者事件模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆