Skip to content

Commit

Permalink
Merge pull request #15 from thoov/dynamic-routes
Browse files Browse the repository at this point in the history
[BUGFIX] Fixing issue w/ dynamic routes
  • Loading branch information
thoov committed Feb 11, 2015
2 parents 6eb0f9f + fdb311b commit 324752d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
20 changes: 17 additions & 3 deletions addon/mixins/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,24 @@ export default Ember.Mixin.create({
},

/*
* When the route deactivates or "transitions away" we will either close the
* connection or keep it "alive"
* When the route deactivates (ie transitions away) or resets (ie a dynamic segment of a route is updated)
* we will either close the connection or keep it "alive". This logic used to be contained
* within the deactivate method but if you have a single route with a dynamic segment than
* the "deactive" method is never called. EX: if you have something like this:
*
* updateSocketURL: function(roomID) {
* this.set('socketURL', 'ws://localhost:8080/room/%@'.fmt(roomID));
* },
*
* setupController: function(controller, model) {
* this.updateSocketURL(model.id);
* this._super.apply(this, arguments);
* }
*
* And if you transitioned from room/1 to room/2. The room/1 socket would not close thus we needed to
* place this logic on the resetController.
*/
deactivate: function() {
resetController: function() {
var socketContexts = this.get('socketContexts');
var keepSocketAlive = this.get('keepSocketAlive');
var socketConfigurations = this.get('socketConfigurations');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-websockets",
"version": "0.5.1",
"version": "0.6.0",
"description": "EmberJS WebSockets addon for both Ember-CLI & non CLI Ember apps.",
"directories": {
"doc": "doc",
Expand Down
9 changes: 9 additions & 0 deletions tests/dummy/app/controllers/sockets/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Ember from 'ember';

export default Ember.Controller.extend({

actions: {
onopen: function() {},
onclose: function() {}
}
});
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Router.map(function() {
this.resource('sockets', function() {
this.route('chatroom');
this.route('multichat');
this.route('dynamic', {path: 'dynamic/:room_id'});
});

// Used for intergration tests
Expand Down
20 changes: 20 additions & 0 deletions tests/dummy/app/routes/sockets/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Ember from 'ember';
import socketMixin from 'ember-websockets/mixins/sockets';

export default Ember.Route.extend(socketMixin, {

socketURL: null,

updateSocketURL: function(roomID) {
this.set('socketURL', 'ws://localhost:8080/room/%@'.fmt(roomID));
},

model: function(params) {
return {id: params.room_id};
},

setupController: function(controller, model) {
this.updateSocketURL(model.id);
this._super.apply(this, arguments);
}
});
3 changes: 3 additions & 0 deletions tests/dummy/app/templates/sockets/dynamic.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<br>
{{link-to '123 Dynamic Route' 'sockets.dynamic' 123}}
{{link-to '321 Dynamic Route' 'sockets.dynamic' 321}}

0 comments on commit 324752d

Please sign in to comment.