question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating empty Restangular elements?

See original GitHub issue

Hi,

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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

5reactions
daviesgeekcommented, Jun 16, 2016

From reading the original example, it looks like you should be able to do this:


var building_2 = {}
Restangular.restangularizeElement(null, building_2, 'building')
building_2._getLength(2)

http://plnkr.co/edit/FNwFGjTnAfC4lHasGiB5?p=preview

1reaction
rhys-vdwcommented, Jun 8, 2015

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:

Configurer.setOnElemRestangularized(function(elem, isCollection, what, Restangular) {
  var collection;
  if (isCollection) {
    collection = elem;
    collection.create = function(properties) {
      var instance = Restangular.restangularizeElement(
        collection.parentResource,
        properties || {},
        collection.route
      );
      return instance;
    };
  }
  return elem;
});

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found