Skip to content

Commit

Permalink
Merge pull request Wildhoney#39 from shopapps/patch-1
Browse files Browse the repository at this point in the history
Update application.js
  • Loading branch information
Wildhoney committed Sep 17, 2014
2 parents 734a029 + d89ab9c commit 937ce37
Showing 1 changed file with 116 additions and 1 deletion.
117 changes: 116 additions & 1 deletion example/app/adapters/application.js
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 }
});
}


});

0 comments on commit 937ce37

Please sign in to comment.