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

Problem with adding to collection and deleting a model (alloy)

$
0
0

Dear Titanium friends,

somehow i am not able to add to a collection or deling some models. Please let me try to explain.

I am making an app to present questions. the questions must be save to an sql and after finishing they must be deleted. As i understood i need to have an collection. And in the collections there will be models saved. So every question will be a model and several models will be the collection. Great. I can save a model but a cannot add them to a collection. (error below) After saving to the model it will expand every time. I need to delete them too. Here what i did:

I have this in alloy.js

Alloy.Collections.questions = Alloy.createCollection('questions');
my model:
exports.definition ={
    config:{
        "columns": {
            "id": "INTEGER PRIMARY KEY AUTOINCREMENT",
            "questionID": "INTEGER",
            "question": "TEXT",
            "answer":"INTEGER",
        },
        adapter: {
            type: "sql",
            collection_name: "questions",
            idAttribute: "id"
        }
    },
    extendModel: function(Model) {      
        _.extend(Model.prototype, {
 
            // extended functions go here
 
        }); // end extend
 
        return Model;
    },
 
 
    extendCollection: function(Collection) {        
        _.extend(Collection.prototype, {
 
            // extended functions go here           
 
        }); // end extend
 
        return Collection;
    }
 
};
First row in my controller
var _questionsSQL = Alloy.Collections.questions;
Same controller but in a function (The i is a for loop which works correct):
var model = Alloy.createModel('questions',{
    questionID:_args.questions[i].question_id,
    question:_args.questions[i].question,
    answer:0
});
 
_questionsSQL.add(model);
model.save();
After running the code the studio crashes (no error) here: backbone.js
for (i = 0, length = models.length; i < length; i++) {
        (model = models[i]).on('all', this._onModelEvent, this);
        this._byCid[model.cid] = model;
        if (model.id != null) this._byId[model.id] = model;
      }
PROBLEM 1 if i comment _questionsSQL.add(model); i can save the model. it is adding it to the model every time.

result from: Ti.API.info(model);

{
    "_changing" = 0;
    "_escapedAttributes" =     {
    };
    "_pending" =     {
    };
    "_previousAttributes" =     {
        0 =         {
            answer = 0;
            id = 53;
            question = "Acceptance testing of equipment is done when?";
            questionID = 165;
        };
        1 =         {
            answer = 0;
            id = 54;
            question = "What do you consider to be an installation part?";
            questionID = 151;
        };
 
        answer = 0;
        question = "What purpose serves as built information";
        questionID = 171;
    };
    "_silent" =     {
    };
    "_validate" = "<KrollCallback: 0x7abd24b0>";
    attributes =     {
        0 =         {
            answer = 0;
            id = 53;
            question = "Acceptance testing of equipment is done when?";
            questionID = 165;
        };
        1 =         {
            answer = 0;
            id = 54;
            question = "What do you consider to be an installation part?";
            questionID = 151;
        };
 
        answer = 0;
        question = "What purpose serves as built information";
        questionID = 171;
    };
    bind = "<KrollCallback: 0x7abce850>";
    change = "<KrollCallback: 0x7abd21a0>";
    changed =     {
    };
    changedAttributes = "<KrollCallback: 0x7abd22b0>";
    cid = c4;
    clear = "<KrollCallback: 0x7abce260>";
    clone = "<KrollCallback: 0x7abcf000>";
    config =     {
        adapter =         {
            "collection_name" = questions;
            "db_name" = "_alloy_";
            idAttribute = questionID;
            type = sql;
        };
        columns =         {
            answer = INTEGER;
            id = "INTEGER PRIMARY KEY AUTOINCREMENT";
            question = TEXT;
            questionID = INTEGER;
        };
        defaults =         {
            answer = 0;
            question = "";
            questionID = 0;
        };
    };
    constructor = "<KrollCallback: 0x7abc66a0>";
    defaults =     {
        answer = 0;
        question = "";
        questionID = 0;
    };
    destroy = "<KrollCallback: 0x7abd1e20>";
    escape = "<KrollCallback: 0x7abcf2f0>";
    fetch = "<KrollCallback: 0x7abd1d30>";
    get = "<KrollCallback: 0x7abcf280>";
    has = "<KrollCallback: 0x7abcf360>";
    hasChanged = "<KrollCallback: 0x7abd2220>";
    id = 171;
    idAttribute = questionID;
    initialize = "<KrollCallback: 0x7abce200>";
    isNew = "<KrollCallback: 0x7abcf0f0>";
    isValid = "<KrollCallback: 0x7abd2430>";
    length = 6;
    off = "<KrollCallback: 0x7abce760>";
    on = "<KrollCallback: 0x7abce700>";
    parse = "<KrollCallback: 0x7abd1f00>";
    previous = "<KrollCallback: 0x7abd2330>";
    previousAttributes = "<KrollCallback: 0x7abd23b0>";
    save = "<KrollCallback: 0x7abd1da0>";
    set = "<KrollCallback: 0x7abcf3d0>";
    sync = "<KrollCallback: 0x7abd12f0>";
    toJSON = "<KrollCallback: 0x7abcf210>";
    trigger = "<KrollCallback: 0x7abce7e0>";
    unbind = "<KrollCallback: 0x7abcf050>";
    unset = "<KrollCallback: 0x7abd1c30>";
    url = "<KrollCallback: 0x7abd1e90>";
}
PROBLEM 2

I need to delete the models but somehow i cannot delete them. I tried several options read on google like this 2 (but i tried more)

http://stackoverflow.com/questions/17290882/how-to-use-localstorage-in-titanium-alloy https://github.com/dyerrington/alloy-examples/tree/master/Alloy%20SQL%20Model%20Example/app

Also this page (and subpages) couldn't help me http://docs.appcelerator.com/titanium/3.0/#!/guide/Alloy_Models

My config:

  • Application Type: Mobil
  • Titanium SDK: Titanium Command-Line Interface, CLI version 3.4.2, Titanium SDK version 3.5.0.GA
  • Platform and Verion: IOS 8.3
  • Testdevice: IOS Simulator iPad 2 and iPad Air
  • Host Operating system: Mac 10.10.2
  • Titanium Studio: build: 3.4.1.201410281727

Anyone some good advice?


Viewing all articles
Browse latest Browse all 8068

Trending Articles



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