restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Change URL of Element

Open meenie opened this issue 13 years ago • 13 comments

Hey Again,

I have a scenario where I've done a search for a row of data in a table using Restangular.all('filters/search'). I then display that in a table and give the option to copy an element to another collection of elements I've pulled from my datasource using Restangular.all('filters'). Now I have a collection of elements where one of them has the URL of /filters/search and the rest /filters. If I wanted to further manipulate that new element, I need to somehow change its url to /filters so it will post to the correct URL.

Does this make sense?

Cheers, meenie

meenie avatar Jun 05 '13 05:06 meenie

Hey again,

I don't think you should be actually changing the URL of a resource. A resource is mapped to a certain URL to which it belongs.

What you want is to create a new Resource based on the other.

For that, I'd create first a restangular object first and then set the values from the other instance.

Something like:

var object = Restangular.one('filter', 1);

// This will set in object all values that are in filterSearchObject but are undefined in object
_.defaults(object, filterSearchObject)

What do you think about this approach? I think this makes more sense rather changing the URL. Changing URL IMO is changing the resource.

mgonto avatar Jun 05 '13 05:06 mgonto

Ya, that makes total sense. Thanks :+1:.

I hope it's okay that I'm using GitHub for "Support" heh. Seems to be the best area to ask these questions.

Cheers, meenie

meenie avatar Jun 05 '13 05:06 meenie

Hey,

Yeah, no problem!

mgonto avatar Jun 05 '13 05:06 mgonto

I have a followup question with this as well. How do I create a new resource without using an ID and post that to my datasource so it can generate the PrimaryKey and then return it back to me so I can assign it to the resource for further manipulation?

Basically in the search I want to either move the element into my current list of elements or actually create a new one based on the original and copy it into my existing list of elements. So I need the database to generate the PrimaryKey.

I think after I get this concept figured out in Restangular, I've got everything I need to be a ninja :+1:.

meenie avatar Jun 05 '13 05:06 meenie

var cars = Restangular.all('cars');
cars.post({name: "Mustang"}).then(function(newCar) {
  // Here new car has the ID as it has come from the server.
});

This post method is available in lists obtained from Restangular as well.

mgonto avatar Jun 05 '13 05:06 mgonto

@mgonto, okay, I was actually doing it like this but it seemed wrong because it's not really 'all' because it's only just one element as opposed to an array of elements.

Thanks!

meenie avatar Jun 05 '13 05:06 meenie

You're actually posting a new element, to a list of elements.

.all represents a list of resources. So you're saying add this new element to the list of resources.

mgonto avatar Jun 05 '13 05:06 mgonto

The .all notation creates a "fictional" list of resources. A post there creates a new element. It's like doing a POST to /cars but by code.

Single element also have post methods but it works differently

oneCar.post('drivers', {name: "gonto"} In this case you're telling the car to add a new driver to its list of drivers, so it's also adding an element to a list of something.

mgonto avatar Jun 05 '13 05:06 mgonto

Aha! Got it :). .all('filters') represents the filters table in my database :). Got it! Makes much more sense now :).

meenie avatar Jun 05 '13 05:06 meenie

That's precisely right :)

mgonto avatar Jun 05 '13 05:06 mgonto

I don't understand how this answer solves the problem: how do you get a Restangular list of "filters" from the "fitlers/search" url, such that you could do the following:

var filters = Restangular.all('filters/search').getList();
filters[0].remove(); //this will be an error because it will try to do "DELETE filters/search/1"

Meekohi avatar Feb 04 '16 00:02 Meekohi

@Meekohi agree with you, and I'm having the same issue

gustperz avatar Sep 09 '16 19:09 gustperz

@gustperz For what it's worth, we ended up switching to just using ngResource. This repo is basically abandonware now.

Meekohi avatar Sep 09 '16 20:09 Meekohi