Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:05:09+00:00 2026-06-10T16:05:09+00:00

jQuery tiny PubSub is great when passing primitive values or objects, but has some

  • 0

jQuery tiny PubSub is great when passing primitive values or objects, but has some trouble with arrays. So I have to wrap arrays into an object.

(function($) {
  var o = $({});
  $.subscribe = function() {
    o.on.apply(o, arguments);
  };
  $.unsubscribe = function() {
    o.off.apply(o, arguments);
  };
  $.publish = function() {
    o.trigger.apply(o, arguments);
  };
}(jQuery));
$.subscribe('test',function(e,data){
    console.log(data);
})
$.publish('test',1);       //1
$.publish('test',{a:1});   //{a:1}
$.publish('test',[2,3,4]); //2
$.publish('test',{arr:[2,3,4]})  //{arr:[2,3,4]}

I’ve seen some improve versions of it, which mainly focus on caching subscribers, but none of them can pass arrays. So, two questions:

  • Is it a good idea to pass arrays via PubSub?
  • How to do that?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:05:11+00:00Added an answer on June 10, 2026 at 4:05 pm

    Alright, I figure it out anyway.

    Even thought it may not be a problem to others, but not being able to pass arrays via PubSub is very confusing and inconvenient to me. So I decide to write my own PubSub, instead of using jQuery’s custom events.

    (function (Global) {
        var cache = {};
        Global.PubSub = Global.PubSub || {
            on: function (e, fn) {
                if (!cache[e]) {
                    cache[e] = [];
                }
                cache[e].push(fn);
            },
            off: function (e, fn) {
                if (!cache[e]) {
                    return;
                }
                var fns = cache[e];
                if (!fn) {
                    fns.length = 0;
                }
                var index = fns.indexOf(fn);
                if (index !== 0) {
                    fns.splice(index, 1);
                }
            },
            trigger: function (e, data) {
                if (!cache[e]) {
                    return;
                }
                var fns = cache[e];
                for (var i = 0; i < fns.length; ++i) {
                    fns[i](e, data);
                }
            }
        };
    })(typeof window !== 'undefined' ? window : this);
    PubSub.on('test', function (e, data) {
        console.log(data);
    });
    PubSub.trigger('test', 1);
    PubSub.trigger('test', {
        a: 1
    }); //{a:1}
    PubSub.trigger('test', [2, 3, 4]); //[2,3,4]
    PubSub.trigger('test', {
        arr: [2, 3, 4]
    }); //{arr:[2,3,4]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jquery Tiny Carousel for sliding images but our client asks for
I have a tiny javascript which uses JQUERY UI. When I get the 'drop'
I have only used jQuery UI a tiny amount and do not know it
I have put together a couple of jquery carousel's using either Tiny Carousel or
I'm using the jQuery Validation plugin on my forms. I have some groups of
I have a teeny tiny little issue concerning a jQuery-based slideshow in Opera. What
I am trying to learn some JQuery, mainly for fun, but also for seriouz.
I want to implement and customize Tiny MCE jquery version. Don't know how to
I have a jQuery accordion (jQuery 1.3.2, jQuery UI 1.7.2), with between 3-12 content
I have a div with another div holding three icons from JQuery UI. Inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.