1

Below code doesn't work. When I try to use this module it hasn't got these methods. Looks like I'm returning wrong object?

define([
    'jquery'
], function($ ){

    var o = $({});

    $.subscribe = function() {
        o.on.apply(o, arguments);
    };

    $.unsubscribe = function() {
        o.off.apply(o, arguments);
    };

    $.publish = function() {
        o.trigger.apply(o, arguments);
    };

    return o;
});
4

2 回答 2

2

You probably should return $ (the jQuery object) since you want to use the methods you attached to it like this:

define(['pubsub'], function(pubSub) {
    pubSub.subscribe('test', function(ev) {});
});

With your solution you'd have to do:

define(['jquery', 'pubsub'], function($, o) {
    $.subscribe('test', function(ev) {});
});
于 2013-05-22T17:10:49.423 回答
1

Fixed the issue, I was returning the wrong object as I suspected. I need to return jQuery object. Code below works:

define([
    'jquery'
], function($ ){

    var o = $({});

    $.subscribe = function() {
        o.on.apply(o, arguments);
    };

    $.unsubscribe = function() {
        o.off.apply(o, arguments);
    };

    $.publish = function() {
        o.trigger.apply(o, arguments);
    };

    return $;
});
于 2013-05-22T17:12:46.687 回答