Creating empty Restangular elements?
See original GitHub issueHi,
I love Restangular very much. Last time I hit an edge case using it and this is maybe my misconception about how it works. Please close this if I don’t get this right.
I’m using Restangular.extendModel to add methods for changing values acording to system for handling units. The problem is that this works fine if I get objects from server. If I want to create object in browser then until I send object and get back restangularized object then I’m without that methods from model.
Can I create restangular element that won’t be send?
So imagine something like this (this code could have some errors, I just write it to show use case):
Restangular.extendModel('building', function (model) {
model._getLength = function (value) {
if (service.isImperial()) {
return value * 23.5
} else {
return value;
}
};
}
var building = Restangular.one('building', 1).get();
building._getLength(2); // works
var building_2 = {}; // How to create object without sending it to server??
building_2._getLength(2) // doesn't work
Restangular.all('building').post(building_2).then(function(obj){
building_2 = obj;
building_2._getLength(2) // work again
}); // Until POST I don't have method from models
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:8
Top Results From Across the Web
javascript - Restangular POST always empty
I'm the creator of Restangular. Posts should be done to collections, not to elements. ... The signature is path to subelement collection, element...
Read more >Javascript – Restangular POST always empty
I think I'm not understanding how a POST is done in a RESTful api. When creating a new object in Restangular with this:...
Read more >restangular
How to create a Restangular service with a different ... Restangular methods; Element methods; Collection methods; Custom methods.
Read more >Restangular on Angular - ng-newsletter
To use Restangular, there are two ways we can create an object to fetch services. We can either set the base route to...
Read more >How to make empty an array using AngularJS
Given an array & the task is to make empty an array or delete all the elements from the array in AngularJS. In...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
From reading the original example, it looks like you should be able to do this:
http://plnkr.co/edit/FNwFGjTnAfC4lHasGiB5?p=preview
This is a big hole in the Restangular API, as far as I can tell. The most confusing thing about Restangular is that it presents a
save
method which is meant to automatically determine whether to use PUT or POST, but has no way of creating models that don’t already exist…This is my patch, but it’s imperfect:
However it generates incorrect routes in some cases, so it obviously needs some work. If you can get this approach working I’d love to hear what I’m doing wrong.