forked from Wildhoney/EmberSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Wildhoney#39 from shopapps/patch-1
Update application.js
- Loading branch information
Showing
1 changed file
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,116 @@ | ||
// Adapter... | ||
/** | ||
* ember-sockets adapater script | ||
* app/adapters/application.js | ||
* by Paul Robbins (ShopApps Ltd) created on 2014-09-15 | ||
* www.shopapps.co.uk | ||
* [email protected] | ||
* | ||
* NOTES: This is a ember-cli file used to create a socket adapter | ||
to use, change the line in the init: function where it says window.Myapp.__container__ | ||
so that Myapp is the name of your App | ||
*/ | ||
import Ember from 'ember'; | ||
import DS from "ember-data"; | ||
|
||
export default DS.Adapter.extend({ | ||
needs: ['application'], | ||
init: function() { | ||
console.log("STARTING SOCKET ADAPTER"); | ||
/* | ||
* Put your module name here (relace Myapp). | ||
* todo: find a better method of getting the socket reference... | ||
*/ | ||
this.socket = window.Myapp.__container__.lookup('socket:Main'); | ||
this._super(); | ||
}, | ||
|
||
makeSocketRequest : function (store, eventType, query) { | ||
|
||
var adapter = this; | ||
/* | ||
* todo: better manage when a socket is not yet connected | ||
* need some kind of setTimeout or similar? | ||
*/ | ||
return new Ember.RSVP.Promise(function (resolve, reject) { | ||
try { | ||
if(!adapter.socket.socket.connected) | ||
{ | ||
console.log('[Adapter] App.Socket is not yet connected'); | ||
reject({ message : 'App.Socket is not yet connected'}); | ||
} | ||
else | ||
{ | ||
if (adapter.socket.authenticated) { | ||
adapter.socket.emit(eventType, query, function (data) { | ||
console.log('[Adapter] response?', data); | ||
if (data.status) { | ||
resolve(data.content); | ||
} else { | ||
reject(data); | ||
} | ||
}); | ||
} else { | ||
console.log('[Adapter] App.Socket is not authenticated'); | ||
reject({ message : 'App.Socket is not authenticated'}); | ||
} | ||
} | ||
} catch (e) { | ||
console.error('[Adapter] App.Socket is not available for requests', e); | ||
reject({ message : 'App.Socket is not available for requests'}); | ||
} | ||
}); | ||
|
||
|
||
}, | ||
|
||
find: function (store, type, id) { | ||
console.log('[Adapter] FIND: ', type.typeKey); | ||
return this.makeSocketRequest(store, 'find', { | ||
type : type.typeKey, | ||
query : { id : id } | ||
}); | ||
}, | ||
|
||
|
||
findAll: function(store, type, id) { | ||
console.log('[Adapter] FINDALL: ', type.typeKey, id); | ||
return this.makeSocketRequest(store, 'findAll', { | ||
type : type.typeKey, | ||
query : { since : id } | ||
}); | ||
|
||
|
||
}, | ||
findQuery: function(store, type, query) { | ||
return this.makeSocketRequest(store, 'findQuery', { | ||
type : type.typeKey, | ||
query : query | ||
}); | ||
}, | ||
findMany: function(store, type, ids, records) { | ||
return this.makeSocketRequest(store, 'findMany', { | ||
type : type.typeKey, | ||
query : { ids : ids } | ||
}); | ||
}, | ||
findBelongsTo: function(store, record, url) { | ||
var id = this.get(record, 'id'); | ||
var type = record.constructor.typeKey; | ||
console.log('[Adapter] findBelongsTo: ', type, url); | ||
return this.makeSocketRequest(store, 'findBelongsTo', { | ||
type : type, | ||
query : { id : id } | ||
}); | ||
}, | ||
findHasMany: function(store, record, url) { | ||
var id = this.get(record, 'id'); | ||
var type = record.constructor.typeKey; | ||
console.log('[Adapter] findHasMany: ', type, url); | ||
this.makeSocketRequest(store, 'findHasMany', { | ||
type : type, | ||
query : { id : id } | ||
}); | ||
} | ||
|
||
|
||
}); |