Fill <select> with OPTIONS request


#1

In my project, I’m using Ember.js and Django + django-rest-framework.

Some of my models have numeric fields, but with a limited number of options. With django-rest-framework I get this when querying a user with a GET request (simplified output):

{
    "id": 2, 
    "username": "tapia",
    "gender": 1, 
    "eyes": 1, 
}

If I make an OPTIONS request to the same URL, I get this:

{
    "name": "User Detail", 
    "description": "", 
    "renders": [
        "application/json", 
        "text/html"
    ], 
    "parses": [
        "application/json", 
        "application/x-www-form-urlencoded", 
        "multipart/form-data"
    ], 
    "actions": {
        "PUT": {
            "username": {
                "type": "string", 
                "required": true, 
                "read_only": false, 
                "label": "nombre de usuario", 
                "help_text": "Requerido. 30 caracteres o menos. Letras, n\u00fameros y @/./+/-/_", 
                "max_length": 30
            }, 
            "gender": {
                "type": "multiple choice", 
                "required": true, 
                "read_only": false, 
                "choices": [
                    {
                        "display_name": "Male", 
                        "value": 1
                    }, 
                    {
                        "display_name": "Female", 
                        "value": 2
                    }
                ]
            },
            "eyes": {
                "type": "multiple choice", 
                "required": true, 
                "read_only": false, 
                "choices": [
                    {
                        "display_name": "Brown", 
                        "value": 1
                    }, 
                    {
                        "display_name": "Green", 
                        "value": 2
                    }, 
                    {
                        "display_name": "Black", 
                        "value": 3
                    }, 
                    {
                        "display_name": "Blue", 
                        "value": 4
                    }, 
                    {
                        "display_name": "Gray", 
                        "value": 5
                    }, 
                    {
                        "display_name": "Amber", 
                        "value": 6
                    }, 
                    {
                        "display_name": "Other", 
                        "value": 7
                    }
                ]
            }
        }
    }
}

In Ember, I’m writing a form to edit a user. It would be amazing if I could use the OPTIONS request to fill the element in the form automatically with the available options.

Is it possible in any way?


#2

What about a property like this in your controller:

availableOptions: function(){
  $.ajax({
    url: "http://mysite.com/my/endpoint",
    type: 'options',
    success: function (data) {
      //parse, filter, and return/set data here
    }
  });
}.property()

Or maybe having that as a function in your controller that updates a property. Point is, it’s probably most useful to you to handle this with regular 'ole AJAX.