Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Titanium REST adapter - mapping JSON response to a collection for display

$
0
0

I am using the REST api adapter at https://github.com/viezel/napp.alloy.adapter.restapi/blob/master/restapi.js for my alloy based application. I am trying to get a list of users via a REST call and display the list on a page. I am able to make the REST call via the adapter and get the JSON response. I am not sure how / where to map the JSON response to a model/collection to display the list. I read somewhere that i need to use the "parse" function in the model but not sure how to do it. Should i do it in the model or in the controller?

Note: If i don't use this REST adapter and just do a REST call directly from my controller, i can parse the JSON string, create a model, set the values in the model and add it to the collection. Then use, collection.fetch to display the results. But i can't do this when i use the REST adapter.

Here is my code.

Model user.js:

I am trying to use the parse function and able to log the values like Name, Email from the response. But how do i add these models to a collection?

exports.definition = {
    config: {
        columns: {
            FirstName: text,
            LastName: text,
            email: text
        },
        URL: restUrl,
            type : 'POST',
        adapter: {
            type: restapi,
            collection_name: user,
            idAttribute: id
        },
        headers: { // your custom headers
                    Content-Type: application/json; charset=utf-8,
            }
    },
    extendModel: function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
             parse : function(_resp) {
                //
                }
        });
        return Model;
    },
    extendCollection: function(Collection) {
        _.extend(Collection.prototype, {
            // extended functions and properties go here
            parse : function(_resp) {
                        var parsedData = JSON.parse(JSON.stringify(_resp));
                var records = parsedData['records'];
                for(var i = 0; i < records.length; i++) {
                    Ti.API.log("Name is " + records[i].Name);
                    Ti.API.log("Email is " + records[i].Email);
                }
            },
    });
    return Collection;
    },
};
Controller:
var userList = Alloy.createCollection('user');
 
    userList.fetch({
        success : function() {
          Ti.API.log("Yay! Success!");
         //navigating to the results page to display the results
          var win=Alloy.createController('searchResults').getView();
          win.open();
        },
        error : function(e) {
            Ti.API.error("Error creating Lead!");
 
        }
        }
View page (searchResults.xml):

I am using a TableView here and set the Collection to "user".

<Alloy>
    <Collection src="user"/>;
    <Window id="searchResults" class="container">
        <TableView dataCollection="user">
            <TableViewRow>
                <Label id="name" text="{FirstName}"></Label>
            </TableViewRow>
            <TableViewRow>
                <Label id="email" text="{email}"></Label>
            </TableViewRow>
        </TableView>
    </Window>
</Alloy>
Any help is greatly appreciated!

Thanks


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>