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

Delete last model from collection also delete another last element to.

$
0
0

I have to mange 5 rows of history of my table so when click on tableRow i put that entry in history collection if row is exist than delete it and save again. When i have already 5 model's in history collection i want to remove last model and add new model but on deleting last it also delete the another element also

TableView click


var name = e.row.name; var id = e.row.rowid;

var his = Alloy.Collections.history;
if (Alloy.Collections.history.length > 4) {
    _.each(his.models, function(item) {
        if (item.get('term_id') == id) {
            item.destroy();
            var hisModel = Alloy.createModel('history', {
                'term_name' : name,
                'term_id' : id
            });
            hisModel.save();
        } else {
            Alloy.Collections.history.at(Alloy.Collections.history.length - 1).destroy();
            var hisModel = Alloy.createModel('history', {
                'term_name' : name,
                'term_id' : id
            });
            hisModel.save();
            return;
        }
    });
} else {
    if (Alloy.Collections.history.length > 0) {
        _.each(his.models, function(item) {
            if (item.get('term_id') == id) {
                item.destroy();
                var hisModel = Alloy.createModel('history', {
                    'term_name' : name,
                    'term_id' : id
                });
                hisModel.save();
            } else {
                var hisModel = Alloy.createModel('history', {
                    'term_name' : name,
                    'term_id' : id
                });
                hisModel.save();
                return;
            }
        });
    } else {
        Ti.API.info('---- first added');
        var hisModel = Alloy.createModel('history', {
            'term_name' : name,
            'term_id' : id
        });
        hisModel.save();
    }
}

History Adpter.


exports.definition = { config : { columns : { "id" : 'INTEGER PRIMARY KEY AUTOINCREMENT', "term_id" : "TEXT", "term_name" : "TEXT", "city_name" : "TEXT", "state_name" : "TEXT" }, adapter : { type : "sql", collection_name : "history", idAttribute : "term_id" } }, 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; } };


Tax Table adpter on which row click i putthat entry in table history


exports.definition = { config : { columns : { "id" : 'INTEGER PRIMARY KEY AUTOINCREMENT', "term_id" : "TEXT", "term_name" : "TEXT", "city_name" : "TEXT", "state_name" : "TEXT" }, adapter : { type : "sql", collection_name : "taxonomy", db_name : "city.db", db_file : "/city.db", idAttribute : "term_id" } }, extendModel : function(Model) { _.extend(Model.prototype, { // extended functions and properties go here }); return Model; }, extendCollection : function(Collection) { _.extend(Collection.prototype, { deleteAll : function() { var collection = this; var sql = "DELETE FROM " + collection.config.adapter.collection_name; db = Ti.Database.open(collection.config.adapter.db_name); db.execute(sql); db.close(); collection.trigger('sync'); }, });

    return Collection;
}

};


Main Tableview


<Alloy> <Window class="container"> <Button id="btn_det" onClick="clickbtndet" top="21" left="10" height="25" backgroundColor="red"> Details </Button> <TableView id="tblView" dataCollection="tax"> <TableViewRow title="{term_name}" rowid="{term_id}" name="{term_name}"/> </TableView> </Window>

</Alloy>

History talbeView


<Alloy> <Window class="container" id="hiswin"> <TableView id="tblView" dataCollection="history"> <TableViewRow title="{term_name}" rowid="{term_id}" name="{term_name}"/> </TableView> </Window>

</Alloy>


Viewing all articles
Browse latest Browse all 8068

Trending Articles



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