other_frameworks.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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(), // Converse.js has been loaded
  26. connected_deferred = new $.Deferred(), // An XMPP connection has been established
  27. roster_deferred = new $.Deferred(); // The contacts roster has been fetched.
  28. var loaded_promise = loaded_deferred.promise(),
  29. connected_promise = connected_deferred.promise(),
  30. roster_promise = roster_deferred.promise();
  31. // This is the API of the service.
  32. var service = {
  33. 'waitUntilLoaded': _.constant(loaded_promise),
  34. 'initialize': function initConverse(options) {
  35. this.waitUntilLoaded().done(_.partial(this.api.initialize, options));
  36. },
  37. 'waitUntilConnected': _.constant(connected_promise),
  38. 'waitUntilRosterFetched': _.constant(roster_promise),
  39. };
  40. // Here we define the core components of converse.js that will be
  41. // loaded and used.
  42. define("converse", [
  43. "converse-api",
  44. // START: Removable components
  45. // --------------------
  46. // Any of the following components may be removed if they're not needed.
  47. "locales", // Translations for converse.js. This line can be removed
  48. // to remove *all* translations, or you can modify the
  49. // file src/locales.js to include only those
  50. // translations that you care about.
  51. "converse-chatview", // Renders standalone chat boxes for single user chat
  52. "converse-controlbox", // The control box
  53. "converse-bookmarks", // XEP-0048 Bookmarks
  54. "converse-mam", // XEP-0313 Message Archive Management
  55. "converse-muc", // XEP-0045 Multi-user chat
  56. "converse-vcard", // XEP-0054 VCard-temp
  57. "converse-otr", // Off-the-record encryption for one-on-one messages
  58. "converse-register", // XEP-0077 In-band registration
  59. "converse-ping", // XEP-0199 XMPP Ping
  60. "converse-notification", // HTML5 Notifications
  61. "converse-minimize", // Allows chat boxes to be minimized
  62. "converse-dragresize", // Allows chat boxes to be resized by dragging them
  63. "converse-headline", // Support for headline messages
  64. // END: Removable components
  65. ], function(converse) {
  66. service.api = converse;
  67. return deferred.resolve();
  68. });
  69. require(["converse"]);
  70. return service;
  71. });
  72. The above code is a modified version of the file `src/converse.js <https://github.com/jcbrand/converse.js/blob/master/src/converse.js>`_
  73. which defines the converse AMD module and specifies which plugins will go into
  74. this build.
  75. You should replace the contents of that file with the above, if you want such a
  76. service registered. Then, you should run `make build`, to create new build
  77. files in the `dist` directory, containing your new angular.js service.
  78. The above code registers an angular.js module and service, both named ``converse``.
  79. This module should then be added as a dependency for your own angular.js
  80. modules, for example:
  81. .. code-block:: javascript
  82. angular.module('my-module', ['converse']);
  83. Then you can have the converse service dependency injected into
  84. your components, for example:
  85. .. code-block:: javascript
  86. angular.module('my-module').provider('my-provider', function(converse) {
  87. // Your custom code can come here..
  88. // Then when you're ready, you can initialize converse.js
  89. converse.waitUntilLoaded().done(function () {
  90. converse.initialize({
  91. 'allow_logout': false,
  92. 'auto_login': 'true',
  93. 'auto_reconnect': true,
  94. 'bosh_service_url': bosh_url,
  95. 'jid': bare_jid,
  96. 'keepalive': true,
  97. 'credentials_url': credentials_url,
  98. });
  99. // More custom code could come here...
  100. });
  101. });
  102. You might have noticed the ``waitUntilLoaded()`` method being called on the ``converse``
  103. service. This is a special method added to the service (see the implementation
  104. example above) that makes sure that converse.js is loaded and available. It
  105. returns a jQuery promise which resolves once converse.js is available.
  106. This is necessary because with otherwise you might run into race-conditions
  107. when your angular application loads more quickly then converse.js.
  108. Lastly, the API of converse is available via the ``.api`` attribute on the service.
  109. So you can call it like this for example:
  110. .. code-block:: javascript
  111. converse.api.user.status.set('online');