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

Alloy - Backbone - how to save model to collection

$
0
0

I've read all the doc I could find, and as many of the QA posts that seemed relevent, but I'm still not clear how the API for models and collections works. I know that roughly model = record and a group of models = collection. That's about it. Manipulating either one or both is a bit beyond my understanding right now.

I have two models - tracker and page. Tracker is a singleton and parent of page. When a user visits a page, I fire the track() function:

function track(_pageId) {
 
    tracker.fetch(); //singleton created in alloy.js
 
    var userId = tracker.get('user_id');
 
    var pages = Alloy.createCollection('page');
 
    if (tracker.has('user_id')) { 
 
        //find an array of matching records (hint: only one record expected)
        var page = pages.where({
            page_viewed : _pageId,
            fk_user_id : userId
        });
 
        //if no record, make one
        if (page.length === 0) {            
            pages.add({
                view_count : 1,
                visit_timestamp : helper.timestamp(),
                page_viewed : _pageId,
                fk_user_id : userId
            });
        } else {
            //otherwise update model
            var viewCount = page[0].get('view_count');
            viewCount++;
            page[0].save('view_count', viewCount);
        }
debugger;
        pages.sync('create', 'page');
 
    } else {
        helper.log('error: analyze:track: no matching record.');
    }
 
    return;
}
Since I'm learning this as I go, I don't know if this code makes any sense for what I want to do. I do that it's not working. I get an error in the sql.js module when executing pages.sync('create', 'page');:
message = "'undefined' is not an object (evaluating 'model.config.adapter')";
Tracker appears to be working as intended. Page is what I'm having difficulty with. The use case: On any given page, check to see if the user has been here before. If so, update the viewCount. If not, create an new page and add it to the collection with a viewCount=1. Then save it.

Am I close to achieving the use case? I'm struggling with understanding how sync works and what it needs. As always, your help is most appreciated.

Regards.


Viewing all articles
Browse latest Browse all 8068

Trending Articles



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