jdnichollsc / Ionic-Starter-Template

Reinventing the wheel, again! Sorry Ionic Team... but there are many newbies learning on Youtube!
http://market.ionic.io/starters/ionic-starter-template
MIT License
211 stars 106 forks source link

More than one table in model #2

Closed akvalibra closed 7 years ago

akvalibra commented 8 years ago

I have extended your project, i have added another tables into database (Items, Customers, Invoices etc) and created new Items.js, Customers.js (Its same like users.js but modified for this tables) How to modify model.js factory to work with this situation. Ex: Model.Items.getall() or Model.Customerc.getall()

jdnichollsc commented 8 years ago

Hi @akvalibra

Thanks for report this issue, is a very common question.

You need to create services "Items, Customers, Invoices, etc" and then you can inject these services into the Model service, for example:

/app/js/services/items.js

(function () {
'use strict';

     angular
         .module('App')
         .factory('Items', Items);

     Items.$inject = ['$q', '$sqliteService'];
     function Items($q, $sqliteService) {

         return {
             getAll: function () {
                 var query = "Select * FROM Items";
                 return $q.when($sqliteService.getItems(query));
             }
         };
     }
})();

/app/js/services/model.js

(function () {
'use strict';

     angular
          .module('App')
          .factory('Model', Model);

     Model.$inject = ['Users', 'Items'];
     function Model(Users, Items) {

         return {
             Users: Users,
             Items: Items
         };
     }
})();

/app/js/controllers/items.js

(function() {
'use strict';

    angular
        .module('App')
        .controller('ItemsController', ItemsController);

    ItemsController.$inject = ['$scope', 'Model'];
    function ItemsController($scope, Model) {

         Model.Items.getAll().then(function(items){
              console.log(items);
         });
    }
})();

Regards, Nicholls