Last active
December 21, 2015 17:39
-
-
Save osdrv/6342214 to your computer and use it in GitHub Desktop.
Ember.js RESTAdapter for Sails.js websocket transport.
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
(function() { | |
var isNone = Ember.isNone; | |
DS.SailsWebSocketRESTAdapter = DS.RESTAdapter.extend({ | |
bulkCommit: false, | |
serializer: DS.FixedRelationsRESTSerializer, | |
buildURL: function(record, suffix) { | |
var url = [this.url]; | |
Ember.assert("Namespace URL (" + this.namespace + ") must not start with slash", !this.namespace || this.namespace.toString().charAt(0) !== "/"); | |
Ember.assert("Record URL (" + record + ") must not start with slash", !record || record.toString().charAt(0) !== "/"); | |
Ember.assert("URL suffix (" + suffix + ") must not start with slash", !suffix || suffix.toString().charAt(0) !== "/"); | |
if (!Ember.isNone(this.namespace)) { | |
url.push(this.namespace); | |
} | |
url.push(record); | |
if (suffix !== undefined) { | |
url.push(suffix); | |
} | |
return url.join("/"); | |
}, | |
parseErrorMessage: function(error_obj) { | |
var clone_error_obj; | |
clone_error_obj = null; | |
if (!isNone(error_obj) && (!isNone(error_obj.errors))) { | |
clone_error_obj = $.extend({}, error_obj); | |
$.each(clone_error_obj.errors, function(k, v) { | |
if (!isNone(this.message)) { | |
// @FIX_ME: https://github.com/balderdashy/sails/issues/799 | |
eval("this.message = " + this.message + ";"); | |
} | |
}); | |
} | |
return clone_error_obj; | |
}, | |
createRecord: function(store, type, record) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var data = this.serialize(record, { includeId: true }); | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.post(adapter.buildURL(root), data, function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, record, error_obj); | |
deferred.reject(record, error_obj); | |
} else { | |
result = {}; | |
result[root] = obj; | |
adapter.didCreateRecord(store, type, record, result); | |
deferred.resolve(record, result); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
createRecords: function(store, type, records) { | |
return this._super(store, type, records); | |
}, | |
updateRecord: function(store, type, record) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var id = record.get('id'); | |
var data = this.serialize(record); | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.put(adapter.buildURL(root, id), data, function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, record, error_obj); | |
deferred.reject(record, error_obj); | |
} else { | |
result = {}; | |
result[root] = obj; | |
adapter.didUpdateRecord(store, type, record, result); | |
deferred.resolve(record, result); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
updateRecords: function(store, type, records) { | |
return this._super(store, type, records); | |
}, | |
deleteRecord: function(store, type, record) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var id = record.get('id'); | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.delete(adapter.buildURL(root, id), function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, record, error_obj); | |
deferred.reject(record, error_obj); | |
} else { | |
result = {}; | |
adapter.didDeleteRecord(store, type, record, result); | |
deferred.resolve(record, result); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
deleteRecords: function(store, type, records) { | |
return this._super(store, type, records); | |
}, | |
find: function (store, type, id) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.get(adapter.buildURL(root, id), function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, error_obj); | |
deferred.reject(error_obj); | |
} else { | |
result = {}; | |
result[root] = obj; | |
adapter.didFindRecord(store, type, result, id); | |
deferred.resolve(result, id); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
findAll: function(store, type, since) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.get(adapter.buildURL(root), adapter.sinceQuery(since), function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, record, error_obj); | |
deferred.reject(record, error_obj); | |
} else { | |
result = {}; | |
result[adapter.pluralize(root)] = obj; | |
adapter.didFindAll(store, type, result); | |
deferred.resolve(result); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
findQuery: function(store, type, query, recordArray) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.get(adapter.buildURL(root), query, function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, record, error_obj); | |
deferred.reject(record, error_obj); | |
} else { | |
result = {}; | |
result[adapter.pluralize(root)] = obj; | |
adapter.didFindQuery(store, type, result, recordArray); | |
deferred.resolve(result, recordArray); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
findMany: function(store, type, ids, owner) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
var ids = this.serializeIds(ids); | |
var deferred = $.Deferred(); | |
var error_obj; | |
var result; | |
this.getSocket().then(function(socket) { | |
socket.get(adapter.buildURL(root), { | |
ids: ids | |
}, function(obj) { | |
if (!isNone(obj.status)) { | |
error_obj = adapter.parseErrorMessage(obj); | |
adapter.didError(store, type, record, error_obj); | |
deferred.reject(record, error_obj); | |
} else { | |
result = {}; | |
result[adapter.pluralize(root)] = obj; | |
adapter.didFindMany(store, type, result); | |
deferred.resolve(result); | |
} | |
}); | |
}); | |
return deferred; | |
}, | |
init: function () { | |
this._super(); | |
}, | |
getSocket: function() { | |
return window.getSocket(); | |
} | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @4pcbr with what version of Ember.js did you test this out?