0

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)
})
1
  • One sidenote: 'I don't want to expose the collection as a whole to the end users' - it is called 'security through obscurity'. If collection id is such a sensitive number - consider replacing it with random hash (associated with collection id on your backend). Otherwise collection id's could be iterated through to find existing ids Commented Aug 25, 2015 at 15:32

1 Answer 1

1

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)
        }
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

This works - I don't know if it is the preferred solution, nor have I spent anytime diagnosing side effects - at this stage I am getting my app up and running will optimize/productionalize later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.