Created
June 3, 2014 07:18
-
-
Save donaldpipowitch/2cc8147de4b1218e8dc3 to your computer and use it in GitHub Desktop.
Add auto-update functionality to Restangular (only for collection.post, model.get and model.remove in this example)
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
angular.module('extensions.restangular.auto-update', [ | |
'restangular' | |
]); | |
angular.module('extensions.restangular.auto-update') | |
.factory('RestangularAutoUpdate', function RestangularAutoUpdate(Restangular) { | |
// properties | |
var _routes = []; | |
// helper | |
// TODO: setParentList needed for https://github.com/mgonto/restangular/issues/727. | |
function setParentList(restangularizedElement, collection) { | |
return Restangular.restangularizeElement( | |
restangularizedElement.parentResource, | |
restangularizedElement.plain(), | |
restangularizedElement.route, | |
restangularizedElement.fromServer, | |
collection, | |
restangularizedElement.reqParams | |
); | |
} | |
// auto-update | |
Restangular.setOnElemRestangularized(function(elem, isCollection, route) { | |
// only add auto-update to registered routes | |
if (!_.contains(_routes, route)) { return elem; } | |
if (isCollection) { | |
// auto-update on collection.post | |
elem.post = _.wrap(elem.post, function(post, elementToPost, queryParams, headers) { | |
return post(elementToPost, queryParams, headers).then(function(response) { | |
// TODO: setParentList needed for https://github.com/mgonto/restangular/issues/727. | |
elem.push(setParentList(response, elem)); | |
return elem; | |
}); | |
}); | |
} else { | |
// auto-update on model.get | |
elem.get = _.wrap(elem.get, function(get, queryParams, headers) { | |
return get(queryParams, headers).then(function(response) { | |
if (elem.getParentList) { | |
var parentList = elem.getParentList(); | |
// TODO: setParentList needed for https://github.com/mgonto/restangular/issues/727. | |
parentList[parentList.indexOf(elem)] = setParentList(response, parentList); | |
} | |
return elem; | |
}); | |
}); | |
// auto-update on model.remove | |
elem.remove = _.wrap(elem.remove, function(remove, queryParams, headers) { | |
return remove(queryParams, headers).then(function(response) { | |
if (elem.getParentList) { | |
var parentList = elem.getParentList(); | |
parentList.splice(parentList.indexOf(elem), 1); | |
} | |
return elem; | |
}); | |
}); | |
} | |
return elem; | |
}); | |
// methods | |
function addRoute(route) { | |
_routes.push(route); | |
} | |
// service | |
return { | |
addRoute: addRoute | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for sharing this! I've been struggling with this for a few days and this solution (modified a bit) is a neat workaround.
One thought: if i do a
collection.post(newElement)
I expect the new element as return value not the collection (e.g. I'd like to highlight the new element or do something with it (in my case I need the new element because saving to the server assigns the new ID and I have to know that ID) so I've changed line 37-38 toYour mileage may vary but for me it seems more practical.
(I'm not good enough in JS to know if
return tmp
would return the same object reference aselem[elem.indexOf(tmp)]
but this works for me now until mgonto fixes the issue permanently.)Anyway...thanks again for sharing, really saved me some headache :)