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

How to save data to database?

$
0
0

Hi all. I'm trying to implement a database for my app. I'd followed the example from here. It shows no error and is still working. But data is not adding to database. My code is here

alloy.js

Alloy.Collections.messages = Alloy.createCollection('messages');
controller
var messages = Alloy.Collections.messages;
 
function showId(e) {
    if (e.row.model) {
        alert(e.row.model);
    }
}
 
function addTestFighter(e) {
    for(var i=0; i<6; i++){
        var model = Alloy.createModel('messages', {
            message: 'Name ' + i,
            userId: i,
            timstamp :  new Date(),
            avatar: '/images/head'+i+'.jpg'
        });
        messages.add(model);
        model.save();
    }
    messages.fetch();
}
 
messages.fetch();
xml
<Alloy>
    <TableViewRow>
        <Label id="name" text="{message}"/>
        <Label id="nickname" text="{userId}"/>
        <ImageView id ="image" image="{avatar}" ></ImageView>
    </TableViewRow>
</Alloy>
It works and when clicked on row it alerts the data. But this data is not saved to the database file. how to add it?

my model is

exports.definition = {
    config: {
 
            "adapter": {
                "type": "sql",
                "collection_name": "messages",
                "db_file": "/threads.sqlite",
                "db_name": "messages"
            }
    },      
    extendModel: function(Model) {      
        _.extend(Model.prototype, {
            // extended functions and properties go here
        });
 
        return Model;
    },
    extendCollection: function(Collection) {        
        _.extend(Collection.prototype, {
            // extended functions and properties go here
        });
 
        return Collection;
    }
}
Plese help to solve this issue.

Viewing all articles
Browse latest Browse all 8068

Trending Articles



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