1
我用websql來自Web SQL承諾回報率從我的服務
創建我的控制,我的模型,我的服務讓我的數據不確定:
服務
app.project.service('ProjectService', ['$webSql', 'config', function ($webSql, config) {
var db = $webSql.openDatabase('MyApp', '1.0', 'MyApp', 2 * 1024 * 1024);
this.getAll = function (params) {
var projects = [];
db.selectAll("projects").then(function(results) {
for(var i=0; i < results.rows.length; i++){
projects.push(results.rows.item(i));
}
return projects;
})
};
this.create = function (project) {
return db.insert('projects', project).then(function(results) {
console.log(results.insertId);
});
};
this.createTable = function() {
return db.createTable('projects', {
"id":{
"type": "INTEGER",
"null": "NOT NULL", // default is "NULL" (if not defined)
"primary": true, // primary
"auto_increment": true // auto increment
},
"created":{
"type": "TIMESTAMP",
"null": "NOT NULL",
"default": "CURRENT_TIMESTAMP" // default value
},
"name":{
"type": "TEXT",
"null": "NOT NULL"
},
"path": {
"type": "TEXT",
"null": "NOT NULL"
},
"file": {
"type": "TEXT",
"null": "NOT NULL"
}
});
};
}]);
型號
app.project.factory('Project', function (ProjectService) {
var Project = function (properties) {
// Model
this.file = null;
this.name = null;
this.path = null;
this.is_active = null;
angular.extend(this, properties);
};
Project.prototype.setModel = function (obj) {
angular.extend(this, obj);
};
// With service
// ------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------
Project.prototype.getAll = function() {
return ProjectService.getAll();
};
Project.prototype.create = function (project) {
return ProjectService.create(project);
};
Project.prototype.createTable = function() {
return ProjectService.createTable();
};
return Project;
});
控制器
app.home.controller('HomeController', ['$scope', '$http', '$webSql', 'Project', function ($scope, $http, $webSql, Project) {
var project = new Project();
$scope.refresh = function(){
new_project = {"name": 'Project1 WEB PREPROD', "path": 'project1.preprod.site.com', 'file': '/release.txt'};
// project.createTable();
project.create(new_project);
$scope.projects = project.getAll();
console.log(project.getAll());
};
$scope.refresh();
}]);
我可以CREATETABLE並創建一個新的對象(項目),但是當我執行$scope.projects = project.getAll();
我的控制檯對數收益undefined
我在服務嘗試這個
this.getAll = function (params) {
var projects = [];
db.selectAll("projects").then(function(results) {
for(var i=0; i < results.rows.length; i++){
projects.push(results.rows.item(i));
}
}).then(function() {
return projects;
})
};
但我有同樣的問題
但我添加了這個。那麼(函數(){回報的項目;});在我的函數db.selectAll後?我該怎麼做 ? –
@JérémieChazelle內在回報只是連鎖的承諾,第一個地方你還沒有回諾言對象.. –
@JérémieChazelle看看更新..可能是我的代碼不清楚.. –