我被困在这一点上,无法继续。
我正在以这种方式为搜索表单创建 ViewModel:
var viewModel = new SearchViewModel("Api/Vehicle", "Vehicles");
ko.applyBindings(viewModel);
稍后当用户单击查找按钮时,它会触发查找方法(此处简化):
self.Find = function (predicate) {
var query = breeze.EntityQuery
.from(self.actionName)
.where(predicate);
var promisse = self.manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
}
query* 方法仅将数据放入 Knockout.js 可观察数组中:
function querySucceeded(data) {
data.results.forEach(function (item) {
self.results.push(item);
});
return true;
}
function queryFailed(error) {
var erroboj = { message: error.message };
self.errors.push(erroboj);
return false;
}
问题是 querySucceeded 永远不会被调用,并且 Knockout.js 的数据永远不会更新触发数据绑定。
我做错了什么?
这是视图模型。有点不完整和模糊。我想我没有正确理解微风查询的承诺:
/*global $, jQuery, ko, breeze, bootstrap */
function SearchViewModel(serviceName, actionName) {
"use strict";
var self = this;
self.serviceName = serviceName;
self.actionName = actionName;
self.errors = ko.observableArray([]);
self.results = ko.observableArray([]);
self.CreateEntity = function (entityTypeName) {
var entityType = self.manager.metadataStore.getEntityType(entityTypeName),
newEntity = entityType.createEntity();
return self.manager.addEntity(newEntity);
};
function querySucceeded(data) {
// Here the function I want to run
data.results.forEach(function (item) {
self.results.push(item);
});
return true;
}
function queryFailed(error) {
var erroboj = { message: error.message };
self.errors.push(erroboj);
return false;
}
function addError(errorMessage) {
var erroobj = { message: errorMessage };
self.errors.push(erroobj);
}
function NoFilterDefined(Condition) {
var s = "Find(): Funcao Filter() not implemented:";
addError(s);
Condition = null;
throw new Error(s);
}
try {
self.Filter = NoFilterDefined;
self.Find = function () {
debugger;
var Condition = breeze.Predicate;
var predicate;
self.errors.removeAll();
try {
predicate = self.Filter(Condition);
} catch (errorf) {
var sf = "Find(): Error calling Filter() in {actionName}: {error}";
sf = sf.replace("{error}", errorf.message);
sf = sf.replace("{actionName}", predicate.toString());
addError(sf);
throw new Error(sf);
}
var query = breeze.EntityQuery
.from(self.actionName)
.where(predicate);
try {
debugger;
var promisse = self.manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
// Here I suppose the .then(querySucceeded) already was called
} catch (errore) {
var se = "Find(): Erro calling query in {actionName}: {error}";
se = se.replace("{error}", errore.message);
se = se.replace("{actionName}", self.actionName);
addError(se);
throw new Error(se);
}
};
self.FetchMetadata = function () {
//self.manager.fetchMetadata() // not working?
// .fail(self.queryFailed);
var query = breeze.EntityQuery
.from(actionName)
.where("1", "==", "0"); // Just to get metadata
var execution = self.manager.executeQuery(query);
execution.then(function (data) {
data = null;
});
return true;
};
self.SetParameters = function (parameters) {
self.parameter = ko.observable(parameters);
};
self.store = new breeze.MetadataStore(); // define metadataStore for all managers
self.manager = new breeze.EntityManager({
serviceName: self.serviceName,
metadataStore: self.store
});
} catch (error) {
var erroboj = { message: error.message };
self.Errors.push(erroboj);
}
}
try {
// Uncomment //debugger to debug this script. Use Visual Studio an IE to debug.
//debugger;
var viewModel = new SearchViewModel("Api/Vehicle", "Vehicles");
var parameter = {
Id: "AAB-1113",
Type: "CIF"
};
viewModel.SetParameters(parameter);
viewModel.Filter = function (Condition) {
"use strict";
var self = this;
var where = Condition.create("Type", "==", self.parameter.Type);
if (parameter.Id !== null) {
where = where.and("Id", "contains", self.parameter.Id);
}
return where;
};
ko.applyBindings(viewModel);
} catch (error) {
alert("Erro de codigo:".concat(error.message));
}