Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to build my first application with AngularJs. I'm building it for Tizen OS, and I also use Jquery mobile, but that doesn't really matter in this case.

As a persistent storage I have chosen WebSql. I found a nice wrapper as an angular module - angular-websql. I figured out more or less how simple angular controllers work, but all this WebSql stuff is asynchronous, what makes things really complicated for me.

I came out with an approach, but I'm not sure if it is "angular way".

app = angular.module('foo', ["angular-websql"])
.factory('User', function ($webSql) {
    db = $webSql.openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
    db.createTable('users', {
       // table schema
    });

    User = {};
    User.insert = function(elem, callback) {
        db.insert('expenses', elem, callback);
    }
    User.selectAll = function(handler, callback){
        db.selectAll('expenses', function(results) {
            for(var i=0; i < results.rows.length; i++) {
             handler(results.rows.item(i));
            }
            callback();
        });
    }
    return User;
})

.controller('BarCtrl', function ($scope, User) {
    $scope.add = function () {
      User.insert({
         name: $scope.name,
         lastname: $scope.lastname
         //and so on
      }, UpdateUsers)
    }

    function UpdateUsers() {
        $scope.users = []
        Foo.selectAll(function(elem) {$scope.users.push(elem)}, $scope.$apply});
    }
 });

As it comes from the code, I have to manually update the expense binding once the record is added to the database (as that action is asynchronous). I also have to call $scope.$apply then. Is there a way to avoid it? Is there a way to use angular promises at least, maybe it makes things better? I feel that this code can be improved, not sure in what direction, though. Any help will be appreciated.

share|improve this question

1 Answer 1

I would say (IMHO) that you should wrap as much of the websql into your service factory. Build out your returned object list in your service and would agree that using a promise would be a good thing to do. When you interact outside of the angular binding, you do need to do scope.apply but probably should do this in the service. Even though there is callback like stuff in angular, I would urge to refrain from introducing this construct in your angular code. It will be much more readable.

For me, i try to be a purist and recommend hiding all third party libraries from your controller code and keep it in your services and directives. By doing this you make code in the NG-way and can make unit testing much easier. You also have the ability to swap out a new library without breaking your already defined contract.

make sense?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.