exports.definition = {
config: {
columns: {
// twitter column
"id_str":"TEXT",
"name":"TEXT",
"screen_name":"TEXT",
"profile_image_url_https":"TEXT",
"profile_background_image_url": "TEXT",
// twitter token for login
"access_token":"TEXT",
"access_token_secret":"TEXT",
// for ACS
"id_str_acs": "TEXT",
// "session_id_acs": "TEXT",
// for yotoo
"active":"INTEGER",
"status_active_tab_index":"INTEGER"
},
adapter: {
// idAttribute: "id_str", // 64bit.. but TEXT
type: "sql",
collection_name: "account"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions go here
testFunction: function (attrs){
Ti.API.info("testFunc: "+ this.get('name')); // it works!
for (var key in attrs) {
var value = attrs[key];
Ti.API.info("testFunction: "+ value);
}
},
createCollection: function(typeOfCollection){
var collection = Alloy.createCollection(typeOfCollection);
collection.twitterApi = this.twitterApi;
return collection;
},
createModel: function(typeOfModel){
var model = Alloy.createModel(typeOfModel);
model.twitterApi = this.twitterApi;
return model;
}
}); // end extend
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions go here
changeCurrentAccount: function(currentAccount){
Ti.API.info("[account.js] will change to " + currentAccount.get('name'));
var isInCollection = false;
Alloy.Globals.accounts.map(function(account){
if(account.get('active')){
account.set('active', false);
account.save();
}
if(account === currentAccount){
Ti.API.info(currentAccount.get('name') +" is in Collection");
isInCollection = true;
}
});
if( !isInCollection ){
Ti.API.info(currentAccount.get('name')+" will added in Collection");
Alloy.Globals.accounts.add(currentAccount);
}
currentAccount.set('active', true);
** 3. 문제가 되는 곳 입니다 **
currentAccount.save();
},
deleteAccount: function (account){
Ti.API.debug("before delete: " + Alloy.Globals.accounts.length);
var currentAccountDeleted = account.get('active');
Alloy.Globals.accounts.remove(account);
account.destroy();
if( currentAccountDeleted && Alloy.Globals.accounts.length > 0){
Alloy.Globals.accounts.changeCurrentAccount( Alloy.Globals.accounts.at(0) );
}
Ti.API.debug("after delete: " + Alloy.Globals.accounts.length);
},
addNewAccount: function (callback){
** 1. 이곳에서 모델을 생성합니다**
var newAccount = Alloy.createModel('account');
var twitterAdapter = require('twitter');
////// var twitterAPI = new TwitterAdapter.Twitter(TwitterAdapter.tokens);
var twitterApi = twitterAdapter.create();
// log in via webView
twitterApi.authorize({
'onSuccess': function(){
// Ti.API.debug("authorize success");
// after log in.
newAccount.set({
'access_token': twitterApi.getAccessTokenKey(),
'access_token_secret': twitterApi.getAccessTokenSecret()
});
// newAccount.set('active', true);
newAccount.set('status_active_tab', 0);
newAccount.twitterApi = twitterApi;
Ti.API.debug("[account.js] accessTokenKey: " + newAccount.get('access_token'));
Ti.API.debug("[account.js] accessTokenSecret: " + newAccount.get('access_token_secret'));
/////var user = Alloy.createModel('User');
/////user.ownerAccount = newAccount;
var user = newAccount.createModel('user');
user.fetchFromServer({
'purpose': 'profile',
'params': {},
'onSuccess': function(){
newAccount.set({
'id_str': user.get('id_str'),
'name': user.get('name'),
'screen_name': user.get('screen_name'),
'profile_image_url_https': user.get('profile_image_url_https').replace(/_normal/g, '_bigger'),
'profile_background_image_url': user.get('profile_background_image_url')
});
Ti.API.info("[account.js] name: " + newAccount.get('name'));
// save new account to persistence store
** 여기서의 .save()는 idAttribute의 지정과 관계없이 잘 동작 합니다 **
newAccount.save(); // must call after callback
Ti.API.info("[account.js] new account saved");
** 2. 이곳에서 생성된 모델을 반환 하구요 **
callback(newAccount);
},
'onFailure': function(){
Ti.API.info("[account.js] user.fetchFromServer failure")
}
}); // user.getUser()
},
'onFailure': function(){
Ti.API.debug("[account.js] fail to add account");
}
}); // twitterAPI.authorize()
} // addNewAccount
}); // end extend
return Collection;
}
}
addAccount 를 통해 얻은 모델을 사용하다가 changeCurrentAccount 에게 모델을 넘겼을때 .save() 가 동작 하지 않아요.
accounts.map(function(account){
var row = Alloy.createController('accountRow', {
"account" : account
}).getView();
row.addEventListener(‘click’, function(e){
$.accountsWindow.close();
if( account !== ownerAccount ){
// change current account
accounts.changeCurrentAccount( account );
}
});
$.accountsTable.appendRow(row);
});
이건 뷰 에서 모델을 사용 하는 부분인데요, 모델 아뎁터의 idAttribute 지정하는 라인을 주석처리하면 잘 작동 합니다