Skip to content

Added UMD support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ npm install --save ng-stomp
```
----

### Install via npm (Webpack & ES6 imports):
```bash
npm install --save ng-stomp
```
```js
import ngStomp from 'ng-stomp';

angular
// Declare ngStomp as a dependency for you module
.module('app', ngStomp)
```
----

## Usage
Inject it in your controller:
```js
Expand Down Expand Up @@ -99,4 +112,4 @@ angular
- on(destination, callback, headers)
- unsubscribe(subscription)
- off(subscription)
- send(destination, body, headers)
- send(destination, body, headers)
4 changes: 1 addition & 3 deletions dist/ng-stomp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions dist/ng-stomp.standalone.min.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"version": "0.2.0",
"description": "STOMP for AngularJS",
"main": "src/ng-stomp.js",
"dependencies": {
"angular": ">= 1.5.0",
"sockjs-client": "1.1.1",
"stompjs": "2.3.3"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-uglify": "^2.0.0",
Expand All @@ -25,5 +30,8 @@
"bugs": {
"url": "https://github.com/beevelop/ng-stomp/issues"
},
"homepage": "https://github.com/beevelop/ng-stomp"
"homepage": "https://github.com/beevelop/ng-stomp",
"standard": {
"global": [ "define" ]
}
}
133 changes: 73 additions & 60 deletions src/ng-stomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,89 @@
* @author Maik Hummel <[email protected]>
* @license MIT
*/
(function (root, factory) {
'use strict'

/*global
angular, SockJS, Stomp */
if (false && typeof define === 'function' && define.amd) {
define(['angular', 'sockjs-client', 'stompjs'], factory)
} else if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports = factory(require('angular'), require('sockjs-client'), require('stompjs'))
} else {
return factory(root.angular, root.SockJS, root.Stomp)
}
}(window, function (angular, SockJS, Stomp) {
'use strict'

angular
.module('ngStomp', [])
.service('$stomp', [
'$rootScope', '$q',
function ($rootScope, $q) {
this.sock = null
this.stomp = null
this.debug = null
var moduleName = 'ngStomp'

this.setDebug = function (callback) {
this.debug = callback
}
angular
.module(moduleName, [])
.service('$stomp', [
'$rootScope', '$q',
function ($rootScope, $q) {
this.sock = null
this.stomp = null
this.debug = null

this.connect = function (endpoint, headers, errorCallback) {
headers = headers || {}
this.setDebug = function (callback) {
this.debug = callback
}

var dfd = $q.defer()
this.connect = function (endpoint, headers, errorCallback) {
headers = headers || {}
var dfd = $q.defer()

this.sock = new SockJS(endpoint)
this.stomp = Stomp.over(this.sock)
this.stomp.debug = this.debug
this.stomp.connect(headers, function (frame) {
dfd.resolve(frame)
}, function (err) {
dfd.reject(err)
errorCallback(err)
})
this.sock = new SockJS(endpoint)
this.stomp = Stomp.over(this.sock)
this.stomp.debug = this.debug
this.stomp.connect(headers, function (frame) {
dfd.resolve(frame)
}, function (err) {
dfd.reject(err)
errorCallback(err)
})

return dfd.promise
}
return dfd.promise
}

this.disconnect = function () {
var dfd = $q.defer()
this.stomp.disconnect(dfd.resolve)
return dfd.promise
}
this.disconnect = function () {
var dfd = $q.defer()
this.stomp.disconnect(dfd.resolve)
return dfd.promise
}

this.subscribe = this.on = function (destination, callback, headers) {
headers = headers || {}
return this.stomp.subscribe(destination, function (res) {
var payload = null
try {
payload = JSON.parse(res.body)
} finally {
if (callback) {
callback(payload, res.headers, res)
this.subscribe = this.on = function (destination, callback, headers) {
headers = headers || {}
return this.stomp.subscribe(destination, function (res) {
var payload = null
try {
payload = JSON.parse(res.body)
} finally {
if (callback) {
callback(payload, res.headers, res)
}
}
}
}, headers)
}
}, headers)
}

this.unsubscribe = this.off = function (subscription) {
subscription.unsubscribe()
}
this.unsubscribe = this.off = function (subscription) {
subscription.unsubscribe()
}

this.send = function (destination, body, headers) {
var dfd = $q.defer()
try {
var payloadJson = JSON.stringify(body)
headers = headers || {}
this.stomp.send(destination, headers, payloadJson)
dfd.resolve()
} catch (e) {
dfd.reject(e)
this.send = function (destination, body, headers) {
var dfd = $q.defer()
try {
var payloadJson = JSON.stringify(body)
headers = headers || {}
this.stomp.send(destination, headers, payloadJson)
dfd.resolve()
} catch (e) {
dfd.reject(e)
}
return dfd.promise
}
return dfd.promise
}
}]
)
}]
)

return moduleName
}))