Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

ember.js - Does EmberJs support publish/subscriber eventing pattern?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
});?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...