2
0

other_frameworks.rst 6.0 KB

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