Titanium JIRA Archive
Alloy (ALOY)

[ALOY-1502] Destroy Model in Alloy doesn't seem to be working

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionInvalid
Resolution Date2016-10-18T19:02:36.000+0000
Affected Version/sRelease 5.2.2
Fix Version/sn/a
ComponentsModels
Labelsalloy, destroy
ReporterAminul Islam
AssigneeFeon Sua Xin Miao
Created2016-06-05T16:42:37.000+0000
Updated2016-10-18T19:04:55.000+0000

Description

Hi, Trying to delete models using destroy() events seems not working. Steps to reproduce : 1. Create a simple Alloy project. 2.Create a model TestUser.js
exports.definition = {
	config: {
		columns: {
			'id': 'INTEGER PRIMARY KEY AUTOINCREMENT',
		    "firstName": "TEXT",
		    "lastName": "TEXT",
		    "street": "TEXT"
		},
		adapter: {
			type: "sql",
			collection_name: "TestModel",
			idAttribute: 'id'
		}
	}
};
3. Copy the following code in index.js
var user = Alloy.createModel('TestUser');
user.save({id:1, firstName:'aa', lastName:'bb', street:'cc'},{
	success:function()
	{
		Ti.API.info('*** user before destroy\n' + JSON.stringify(user,null,2));
		alert('*** user before destroy\n' + JSON.stringify(user,null,2));
		user.destroy({
			success:function()
			{
				Ti.API.info('*** user after destroy\n' + JSON.stringify(user,null,2));
				alert('*** user after destroy\n' + JSON.stringify(user,null,2));
			}
		});
		
	}
});
/*
user.fetch();
user.destroy({
	success:function()
	{
		Ti.API.info('*** user after destroy\n' + JSON.stringify(user,null,2));
		alert('*** user after destroy\n' + JSON.stringify(user,null,2));
	}
});
*/
$.index.open();
4. Run this project Destroy Model in Alloy doesn't seem to be working, because I get same log before and after destroy(); Log
[INFO] :   *** book before destroy

[INFO] :   {

[INFO] :   "title": "aa2",

[INFO] :   "author": "bb2",

[INFO] :   "alloy_id": "3da5ac16-aa00-0e52-727d-9ad551f5c850"

[INFO] :   }

[INFO] :   *** book after destroy

[INFO] :   {

[INFO] :   "title": "aa2",

[INFO] :   "author": "bb2",

[INFO] :   "alloy_id": "3da5ac16-aa00-0e52-727d-9ad551f5c850"

[INFO] :   }

Environment
Operating System
  Name                        = Mac OS X
  Version                     = 10.11.5
  Architecture                = 64bit
  # CPUs                      = 4
  Memory                      = 8.0GB

Node.js
  Node.js Version             = 4.4.4
  npm Version                 = 2.15.1

Appcelerator CLI
  Installer                   = 4.2.6
  Core Package                = 5.3.0

Titanium CLI
  CLI Version                 = 5.0.8
  node-appc Version           = 0.2.31
Titanium SDK : 5.3.0.GA
alloy -v : 1.7.33

Thanks

Comments

  1. Feon Sua Xin Miao 2016-10-18

    model.destroy triggers a DELETE event to the database, it doesn't destroy the model object instance stored in memory. Model objects are garbage collected like everything else in JS, and will go away once they are unreachable. To verify, the row is removed from the table:
       var user = Alloy.createModel('TestUser');
       user.save({id:1, firstName:'aa', lastName:'bb', street:'cc'},{
       	success: function() {
       		Ti.API.info('|************************************');
       		Ti.API.info('*** user before destroy\n' + JSON.stringify(user,null,2));
       
       		var db = Ti.Database.open('_alloy_');
       		var sql = 'SELECT * FROM TestModel';
       		var rs = db.execute(sql);
       		while (rs.isValidRow()) {
       			Ti.API.info(rs.fieldByName('id') + ' ' + rs.fieldByName('firstName') + ' ' + rs.fieldByName('lastName'));
       			rs.next();
       		}
       		rs.close();
       
       		user.destroy({
       			success: function(model, response) {
       				Ti.API.info('*** user after destroy\n' + JSON.stringify(user,null,2));
       				rs = db.execute(sql);
       				while (rs.isValidRow()) {
       					Ti.API.info(rs.fieldByName('id') + ' ' + rs.fieldByName('firstName') + ' ' + rs.fieldByName('lastName'));
       					rs.next();
       				}
       
       				rs.close();
       				db.close();
       				Ti.API.info('************************************|');
       			},
       			error: function(err){
       				Ti.API.info('error');
       				Ti.API.info(err);
       			}
       		});
       	}
       });
       

JSON Source