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

Alloy Backbone - need help with the fundamentals for basic database CRUD

$
0
0
  • app: iOS mobile
  • sdk 3.1.2
  • os: osx mavericks
  • studio Titanium Studio, build: 3.1.3.201309132423
  • output: iPhone Simulator v 7.x

I've been struggling for a good portion of my work day with a basic database concept, CRUD, using Backbone with little progress.

Here's my problem: I want to track every page view on my app using a custom tracker (see my other posts about why I'm using custom analytics). I have a function called track() that passes in the current window ID, and fetches the tracker (master) record. If one exists, query the page (detail) records, increment the view count, and save the page back to the database. Sounds simple enough. Can't get it to work, though.

Here's the function:

function track(_pageId) {
    var viewCount;
    tracker.fetch();
    helper.log("analyze:track:tracker object: " + JSON.stringify(tracker));
    helper.log("analyze:track:_pageId = " + _pageId + ", tracker.user_id " + tracker.user_id);
    if (_pageId & tracker.user_id) {
        page.fetch({
            query: "select view_count from Pages where page_viewed = ? and fk_user_id = ?",
            params: [ _pageId, tracker.user_id ]
        });
        helper.log("analyze:track:queryPage: " + JSON.stringify(page));
        viewCount++;
        page.save({
            view_count: viewCount,
            visit_timestamp: helper.timestamp(),
            page_viewed: _pageId,
            fk_user_id: tracker.user_id
        });
        page.fetch();
        helper.log("analyze:track:savePage: " + JSON.stringify(page));
    } else helper.log("error: analyze:track: no matching record.");
    return;
}
The first cosole call and output which clearly shows user_id: 1
helper.log("analyze:track:tracker object: " + JSON.stringify(tracker));
 
analyze:track:tracker object: 
 
{
   "last_visit":1387328067.487,
   "first_visit":1387328067.487,
   "this_visit":1387328067.487,
   "latitude":null,
   "longitude":null,
   "opted_out":0,
   "opted_in":1,
   "user_id":1,
   "locale":"en",
   "device_model":"Simulator",
   "os_platform":"iPhone OS",
   "os_version":"7.0.3"
}
The cosole call and output immediately following the first:
helper.log("analyze:track:_pageId = " + _pageId + ", tracker.user_id " + tracker.user_id);
analyze:track:_pageId = index, tracker.user_id undefined
tracker.user_id is now "undefined", throws an error, and calls the following code:
helper.log("error: analyze:track: no matching record.");
So, 1) is there an easier way of doing what I want to do, and 2) what am I doing wrong here?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



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