Pregunta

I have a child resource which will have full set of methods associated with it. I am starting to implement the create (i have the back end working). Essentially I want to be able to work with this structure

http://127.0.0.1:8000/rest/parent_resource/<parent_id>/child_resource/

Where is an integer field identifying a parent object. For example:

http://127.0.0.1:8000/rest/parent_resource/87/child_resource/

For security reasons, I don't want to expose the collection as a whole to the end users. So therefore obj.child_resource = [{blah}] is not an approach I am considering. The parent_resource / collection is owned by one user, with contributions (create) to the collection from other users who can update/delete their contribution to a collection.

I am lost as to how to set this up with restangular. I am thinking about messing about with this, but I don't think appending my identifier to the URI is the right way forward:

app.factory('ParentResource', function (Restangular) {
    return {
          getList: function() {
            return Restangular.all('rest/parent_resource').getList()
          }
          , get: _.memoize(
            function (id) {
                return Restangular.one('rest/parent_resource', id).get()
          })
          , createChild: function(parentId, field_1, field_2) {

            var newChild = {
                "field_1": field_1
                , "field_2": field_2
            }

            return Restangular.all('rest/parent_resource/' + parentId + '/childResource').post(newChild )
        }
    }
})

Usage would be something like

ParentResource.get(87).then(function(parentObj){
     parentObj.createChild(1,2)
})
¿Fue útil?

Solución

I am going to investigate this solution:

 return Restangular.one('rest/parent_resource', parentId).all('child_resource').post(newChild)

in this context:

app.factory('ParentResource', function (Restangular) {
    return {
          getList: function() {
            return Restangular.all('rest/parent_resource').getList()
          }
          , get: _.memoize(
            function (id) {
                return Restangular.one('rest/parent_resource', id).get()
          })
          , createChild: function(parentId, field_1, field_2) {

            var newChild = {
                "field_1": field_1
                , "field_2": field_2
            }

              return Restangular.one('rest/parent_resource', parentId).all('child_resource').post(newChild)
        }
    }
})
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top