other_frameworks.rst 6.0 KB

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