Observable RaptorJS Modules

28 views
Skip to first unread message

Ian McBurnie

unread,
Aug 29, 2013, 1:14:22 PM8/29/13
to rapt...@googlegroups.com

I see it's straightforward to make a RaptorJS Class observable, like this in the constructor:

listeners.makeObservable(this);

But what's the recommended way to make Modules (i.e. singletons without a constructor) observable?

Patrick Steele-Idem

unread,
Aug 29, 2013, 1:23:39 PM8/29/13
to rapt...@googlegroups.com
Hi Ian,

Any object can be made observable and the listeners.makeObservable method supports a few variations, including:
  1. makeObservable(obj, proto);
  2. makeObservable(obj);
If you provide the "proto" argument, then the "publish", "subscribe", etc. methods will be applied to the "proto" object (if they haven't already been applied).
If you do not provide the "proto" argument, then the "publish", "subscribe", etc. methods will be applied to "obj" directly.

Option #1 should be used to make a instance of a "class" observable as shown in the following code:
function MyClass() {
 listeners.makeObservable(this, MyClass.prototype)
}

MyClass.prototype = {
}

By updating "proto" and not "obj", performance will be better since "proto" only needs to be updated once.

Option #2 should be used to make a singleton object observable as shown in the following code:
var myModule = {
};
listeners.makeObservable(myModule);

Hopefully that clarifies. Let me know if you still have questions.

Thanks,
Patrick

Ian McBurnie

unread,
Aug 29, 2013, 1:57:57 PM8/29/13
to rapt...@googlegroups.com
I see. So I need to do this outside of the define() block? e.g.

define(
    'some/namespace/my-module', 
    function(require, exports, module) {
        return { //Return the module definition
            sayHello: function() {
                console.log("Hello World!");
            }
        }
    }
);

require('raptor/listeners').makeObservable(require('some/namespace/my-module'));

Is that correct?

Phil Gates-Idem

unread,
Aug 29, 2013, 2:00:15 PM8/29/13
to rapt...@googlegroups.com
This would probably be easier to follow:
define(
   
'some/namespace/my-module',
   
function(require, exports, module) {

       
var myModule = { //Return the module definition

            sayHello
: function() {
                console
.log("Hello World!");
           
}

       
};


       
require('raptor/listeners').makeObservable(myModule);
       
       
return myModule;
   
}
);

Patrick Steele-Idem

unread,
Aug 29, 2013, 2:02:28 PM8/29/13
to rapt...@googlegroups.com
It is better to keep the call inside the "define" block so that you don't unnecessarily load the module. The following will do the trick:
define(
    'some/namespace/my-module', 
    function(require, exports, module) {
        var listeners = require('raptor/listeners');

        var myModule = { 
            sayHello: function() {
                console.log("Hello World!");
            }
        };

        listeners.makeObservable(myModule);

        return myModule; // Return the module definition
    }
);

--Patrick

Ian McBurnie

unread,
Aug 29, 2013, 2:03:21 PM8/29/13
to rapt...@googlegroups.com
Ah yes, of course. Much better! Thanks.
Reply all
Reply to author
Forward
0 new messages