other_frameworks.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
  3. Integrating converse.js into other frameworks
  4. =============================================
  5. Angular.js
  6. ----------
  7. Angular.js has the concept of a `service <https://docs.angularjs.org/guide/services#!>`_,
  8. which is a special kind of `provider <https://docs.angularjs.org/guide/providers>`_.
  9. An angular.js service is a constructor or object which provides an API defined by the
  10. author of the service. The goal of a service is to organize and share code, so
  11. that it can be used across an application.
  12. So, if we wanted to properly integrate converse.js into an angular.js
  13. application, then putting it into a service is a good approach.
  14. This lets us avoid having a global ``converse`` API object (accessible via
  15. ``windows.converse``), and instead we can get hold of the converse API via
  16. angular's dependency injection when we specify it as a dependency for our
  17. angular components.
  18. Below is an example code that wraps converse.js as an angular.js service.
  19. .. code-block:: javascript
  20. angular.module('converse', []).service('converse', function() {
  21. // We create three promises, which will be resolved at various times
  22. var loaded_deferred = new $.Deferred(),
  23. connected_deferred = new $.Deferred();
  24. var service = {
  25. 'waitUntilLoaded': _.constant(loaded_deferred.promise()),
  26. 'initialize': function initConverse(options) {
  27. this.waitUntilLoaded().done(_.partial(this.api.initialize, options));
  28. },
  29. 'waitUntilConnected': _.constant(connected_deferred.promise())
  30. };
  31. // Here we define the core components of converse.js that will be
  32. // loaded and used.
  33. define([
  34. "converse-core",
  35. // START: Removable components
  36. // --------------------
  37. // Any of the following components may be removed if they're not needed.
  38. "locales", // Translations for converse.js. This line can be removed
  39. // to remove *all* translations, or you can modify the
  40. // file src/locales.js to include only those
  41. // translations that you care about.
  42. "converse-chatview", // Renders standalone chatboxes for single user chat
  43. "converse-controlbox", // The control box
  44. "converse-bookmarks", // XEP-0048 Bookmarks
  45. "converse-mam", // XEP-0313 Message Archive Management
  46. "converse-muc", // XEP-0045 Multi-user chat
  47. "converse-vcard", // XEP-0054 VCard-temp
  48. "converse-register", // XEP-0077 In-band registration
  49. "converse-ping", // XEP-0199 XMPP Ping
  50. "converse-notification", // HTML5 Notifications
  51. "converse-minimize", // Allows chatboxes to be minimized
  52. "converse-dragresize", // Allows chatboxes to be resized by dragging them
  53. "converse-headline", // Support for headline messages
  54. // END: Removable components
  55. ], function(converse) {
  56. service.api = converse;
  57. // Register a plugin which resolves `waitUntilConnected` promise.
  58. converse.plugins.add('conversejs-angular-service', {
  59. initialize: function () {
  60. this._converse.api.listen.on('connected', connected_deferred.resolve);
  61. }
  62. });
  63. // Converse.js has been loaded, so we can resolve the `waitUntilLoaded` promise.
  64. return loaded_deferred.resolve();
  65. });
  66. require(["converse"]);
  67. return service;
  68. });
  69. The above code is a modified version of the file `src/converse.js <https://github.com/jcbrand/converse.js/blob/master/src/converse.js>`_
  70. which defines the converse AMD module and specifies which plugins will go into
  71. this build.
  72. You should replace the contents of that file with the above, if you want such a
  73. service registered. Then, you should run `make build`, to create new build
  74. files in the `dist` directory, containing your new angular.js service.
  75. The above code registers an angular.js module and service, both named ``converse``.
  76. This module should then be added as a dependency for your own angular.js
  77. modules, for example:
  78. .. code-block:: javascript
  79. angular.module('my-module', ['converse']);
  80. Then you can have the converse service dependency injected into
  81. your components, for example:
  82. .. code-block:: javascript
  83. angular.module('my-module').provider('my-provider', function(converse) {
  84. // Your custom code can come here..
  85. // Then when you're ready, you can initialize converse.js
  86. converse.waitUntilLoaded().done(function () {
  87. converse.initialize({
  88. 'allow_logout': false,
  89. 'auto_login': 'true',
  90. 'auto_reconnect': true,
  91. 'bosh_service_url': bosh_url,
  92. 'jid': bare_jid,
  93. 'credentials_url': credentials_url,
  94. 'whitelisted_plugins': ['conversejs-angular-service']
  95. });
  96. // More custom code could come here...
  97. });
  98. });
  99. You might have noticed the ``waitUntilLoaded()`` method being called on the ``converse``
  100. service. This is a special method added to the service (see the implementation
  101. example above) that makes sure that converse.js is loaded and available. It
  102. returns a promise which resolves once converse.js is available.
  103. This is necessary because with otherwise you might run into race-conditions
  104. when your angular application loads more quickly then converse.js.
  105. Lastly, the API of converse is available via the ``.api`` attribute on the service.
  106. So you can call it like this for example:
  107. .. code-block:: javascript
  108. converse.api.user.status.set('online');