SQL 精简版离子框架
ionic framework with SQL lite
我正在使用离子框架。
我正在实施以下步骤
1) 当我的应用程序运行时。它将创建数据库和 table
2) 使用登录后,我将数据保存到 mysql 并转到仪表板页面
dbQuery.insertCategory();
$state.go('app.dashboard');
dbQuery 是工厂。
.factory('dbQuery', function ($http, $q, $cordovaSQLite, localstorage) {
return {
insertCategory: function () {
$http({
url: "http://mydoman.comcategory",
method: 'GET',
withCredentials: true,
}).success((function (result) {
var user_id = localstorage.get('user_id');
var query = "INSERT OR IGNORE INTO categories (category_id, user_id, category_name,category_type) VALUES (?,?,?,?)";
var data = '';
result.forEach(function (category) {
$cordovaSQLite.execute(db, query, [category.id,user_id, category.category_name, category.category_type]).then(function (res) {
console.log("insertId");
}, function (err) {
console.dir(err);
});
});
}))
},
哪个工作正常
我在仪表板上显示类别列表,但我什么也没找到。
我进行了调试,发现类别的插入需要时间来插入。
有什么方法可以进行双向数据绑定
谢谢
为什么不使用 promise 而只在成功 promise 触发后重定向到 app.dashboard
?
示例:
dbQuery.insertCategory().then(function(result) {
$state.go('app.dashboard');
}, function(error) {
console.error(error);
});
那么在你的工厂里你可以有这样的东西:
.factory('dbQuery', function ($http, $q, $cordovaSQLite, localstorage) {
return {
insertCategory: function () {
var deferred = $q.defer();
$http({
url: "http://mydoman.comcategory",
method: 'GET',
withCredentials: true,
}).success((function (result) {
var user_id = localstorage.get('user_id');
var query = "INSERT OR IGNORE INTO categories (category_id, user_id, category_name,category_type) VALUES (?,?,?,?)";
var data = '';
result.forEach(function (category) {
isWaiting = true;
$cordovaSQLite.execute(db, query, [category.id,user_id, category.category_name, category.category_type]).then(function (res) {
console.log("insertId");
isWaiting = false;
}, function (err) {
console.dir(err);
isWaiting = false;
});
while(isWaiting) {}
});
deferred.resolve("done");
}))
return deferred.promise;
},
我做的很多只是理论,但它应该给你一个想法。我创建了一个布尔值来使 SQL 命令同步。由于它们是同步的,当循环结束时,您可以 return 您的成功承诺。
您可能还有其他几种方法可以做到这一点。就像也许将 SQL 的东西移动到它自己的函数中并递归地输入它。
让我知道这是否适合您。
我正在使用离子框架。
我正在实施以下步骤
1) 当我的应用程序运行时。它将创建数据库和 table 2) 使用登录后,我将数据保存到 mysql 并转到仪表板页面
dbQuery.insertCategory();
$state.go('app.dashboard');
dbQuery 是工厂。
.factory('dbQuery', function ($http, $q, $cordovaSQLite, localstorage) {
return {
insertCategory: function () {
$http({
url: "http://mydoman.comcategory",
method: 'GET',
withCredentials: true,
}).success((function (result) {
var user_id = localstorage.get('user_id');
var query = "INSERT OR IGNORE INTO categories (category_id, user_id, category_name,category_type) VALUES (?,?,?,?)";
var data = '';
result.forEach(function (category) {
$cordovaSQLite.execute(db, query, [category.id,user_id, category.category_name, category.category_type]).then(function (res) {
console.log("insertId");
}, function (err) {
console.dir(err);
});
});
}))
},
哪个工作正常
我在仪表板上显示类别列表,但我什么也没找到。
我进行了调试,发现类别的插入需要时间来插入。
有什么方法可以进行双向数据绑定
谢谢
为什么不使用 promise 而只在成功 promise 触发后重定向到 app.dashboard
?
示例:
dbQuery.insertCategory().then(function(result) {
$state.go('app.dashboard');
}, function(error) {
console.error(error);
});
那么在你的工厂里你可以有这样的东西:
.factory('dbQuery', function ($http, $q, $cordovaSQLite, localstorage) {
return {
insertCategory: function () {
var deferred = $q.defer();
$http({
url: "http://mydoman.comcategory",
method: 'GET',
withCredentials: true,
}).success((function (result) {
var user_id = localstorage.get('user_id');
var query = "INSERT OR IGNORE INTO categories (category_id, user_id, category_name,category_type) VALUES (?,?,?,?)";
var data = '';
result.forEach(function (category) {
isWaiting = true;
$cordovaSQLite.execute(db, query, [category.id,user_id, category.category_name, category.category_type]).then(function (res) {
console.log("insertId");
isWaiting = false;
}, function (err) {
console.dir(err);
isWaiting = false;
});
while(isWaiting) {}
});
deferred.resolve("done");
}))
return deferred.promise;
},
我做的很多只是理论,但它应该给你一个想法。我创建了一个布尔值来使 SQL 命令同步。由于它们是同步的,当循环结束时,您可以 return 您的成功承诺。
您可能还有其他几种方法可以做到这一点。就像也许将 SQL 的东西移动到它自己的函数中并递归地输入它。
让我知道这是否适合您。