1

I am getting following error while use publishOn and subscribeTo method of knockoutjs

TypeError: ko.observable(...).publishOn is not a function

Here is the script

jsfile1.js

define([
'jquery',
'ko'
], function ($, ko) {
  return component.extend({
      this.id = ko.observable().publishOn('test');
  },
  defaults: {
      template: 'Custom_Test/product/view/test1',
  },

  )
})

jsfile2.js

define([
'jquery',
'ko'
], function ($, ko) {
  return component.extend({
      this.aid = ko.observable().SubscribeTo('test');
  },
  defaults: {
      template: 'Custom_Test/product/view/test2',
  },

  )
})

Please help me to sort it out.

Thanks in advance

2

I think these methods are from a plugin that isn't included in M2, see https://github.com/rniemeyer/knockout-postbox for more info.

Also, a function named initObservable has been created for setting up observables. See this answer for more info.

1

Have you extended (created) those methods? they don't exist by default; http://www.knockmeout.net/2012/05/using-ko-native-pubsub.html

You should set your value like this:

ko.observable(this.id).publishOn('test');

Currently you are setting (publishing) nothing on test

  • I have added as mention in by var shouter = new ko.subscribable(). It is setting properly but unable to get on antoher js. I have updated the question – zed Blackbeard Nov 30 '17 at 14:21
  • I've updated my answer – Quisse Nov 30 '17 at 14:30
  • Shouldn't observables be created with the M2 initObservable function? – Ben Crook Nov 30 '17 at 14:46
  • Perhaps, I'm not aware of this. But in source (for example /Users/tommyquissens/sites/wegro/vendor/magento/module-checkout/view/frontend/web/js/view/minicart.js) they create the isLoading observable straight away. – Quisse Nov 30 '17 at 14:54
  • 1
    From what I can tell it doesn't really matter, I like to do things the Magento way but it doesn't seem like they use it consistently themselves. – Ben Crook Nov 30 '17 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.