gpt4 book ai didi

javascript - BreezeJS - 链接查询

转载 作者:行者123 更新时间:2023-11-30 17:43:51 24 4
gpt4 key购买 nike

假设我们有一个客户对象,它有一个“Foo”集合。我希望我的“getCustomer”函数添加它还没有的所有 Foos,然后返回它自己,作为一个 promise ...

所以我想要一个 promise :获得一个客户,然后将所有缺失的 Foos 添加到该客户,这样当 promise 得到解决时,客户就会拥有所有缺失的 foos。

例子:

// dataservice.js

// returns Q.Promise<breeze.QueryResult>
function getCustomer(custId) {
var query = breeze.EntityQuery().from("Customers").where("CustomerId", "==", custId);
return this.em.executeQuery(query);
}

// returns Q.Promise<breeze.QueryResult>
function getFoosNotOnCustomer(customer) {
var query = breeze.EntityQuery().from("Foo").where("CustomerId", "!=", customer.Id());
return this.em.executeQuery(query);
}

我正在为如何将这些正确地“链接”在一起而苦苦挣扎,如果没有找到客户该怎么办等等。我如何修改“getCustomer”来做到这一点?我基本上是在尝试同步使用 Breeze 。这是我的尝试,但它很快变成了丑陋的嵌套代码。

   // want to return Q.Promise<Customer> that has Foos loaded
// I think this is actually returning something like Q.Promise<Q.Promise<Customer>>
function getCustomer(custId) {
var query = breeze.EntityQuery().from("Customers")
.where("CustomerId", "==", custId);

return this.em.executeQuery(query) // return here?
.then(function(data) {
// what about conditionals?
if(data.results.length == 1) {
getFoosNotOnCustomer(data.results[0]).
then(function (foosArray) {
$.each(foosArray, function(i,el) {
// push foos onto customer instance
}
return custWithFoos; // return here?
}
// return something?
}
}
}

这是我最后做的:

 function getCustomer(custId) {

var query = breeze.EntityQuery().from("Customers").where("CustomerId", "==", custId);

return manager.executeQuery(query) // return here?
.then(addFoos)
.then(doSomethingElse);
}
function addFoos(data) {
var myDefer = Q.Defer();

if (data && data.result.length == 1) {
var customer = data.results[0];
var query = // get FOOS Customer doesn't have;
manager.executeQuery(query).then(function (fooData) {
$.each(fooData.results function (i, el) {
customer.Foos.push(el);
});
myDefer.reslove(customer);
});
} else {
myDefer.resolve(undefined);
}

return myDefer.promise;
}

function doSomethingElse(customer) {
var myDefer = Q.Defer();
customer.SomePropert("test");
return myDefer.resovlve(customer);
}

// ----- MVVM
var custPromise = getCustomer(1).then(function (customer) {
// do something
});

最佳答案

尽管我无法理解语义,但我将以您的面值为例……尤其是我无法理解为什么让所有不属于客户的 Foos 会有帮助。

我只关注“链接”,我假设您希望调用者在您完成后接管选定的客户。

顺序链接

在这个例子中,我们在得到 Foos 之前等待客户

function getCustomer(custId) {

var cust;
var em = this.em;
var query = breeze.EntityQuery().from("Customers")
.where("CustomerId", "==", custId);

// On success calls `gotCustomer` which itself returns a promise
return em.executeQuery(query)
.then(gotCustomer)
.fail(handleFail); // you should handleFail

// Called after retrieving the customer.
// returns a new promise that the original caller will wait for.
// Defined as a nested success function
// so it can have access to the captured `cust` variable
function gotCustomer(data) {
cust = data.results[0];

if (!cust) {
return null; // no customer with that id; bail out now
}
// got a customer so look for non-customer foos
// returning another promise so caller will wait
return breeze.EntityQuery().from("Foos")
.where("CustomerId", "!=", custId)
.using(em).execute()
.then(gotFoos);
}

// Now you have both the customer and the other Foos;
// bring them together and return the customer.
function gotFoos(data) {
var foos = data.results;
// assume `notMyFoos` is an unmapped property that
// should hold every Foo that doesn't belong to this Customer
foos.forEach(function(f) { cust.notMyFoos.push(f); }
return cust; // return the customer to the caller after Foos arrive.
}
}

并行异步查询

在您的场景中,您实际上不必在获取 foo 之前等待客户查询。您从一开始就知道客户和 foos 的选择标准。 假设您认为客户查询返回客户的可能性很高,您可能会同时触发两个查询,然后在两个查询完成时混合数据。考虑 Q.all为此。

function getCustomer(custId) {
var em = this.em;

var custPromise = breeze.EntityQuery().from("Customers")
.where("CustomerId", "==", custId)
.using(em).execute();

var fooPromise = breeze.EntityQuery().from("Foos")
.where("CustomerId", "!=", custId)
.using(em).execute();

Q.all([custPromise, fooPromise])
.then(success)
.fail(handleFail); // you should handleFail

// Now you have both the customer and the "other" Foos;
// bring them together and return the customer.
// `data` is an array of the results from each promise in the order requested.
function success(data) {
var cust = data[0].results[0];
if (!cust) return null;

var foos = data[1].results;

// assume `notMyFoos` is an unmapped property that
// should hold every Foo that doesn't belong to this Customer
foos.forEach(function(f) { cust.notMyFoos.push(f); }
return cust; // return the customer to the caller after Foos arrive.
}
}

请注意,我不必在成功路径中进行如此多的空值检查。当成功回调被调用时,我保证有 data.results。我确实必须考虑没有 CustomercustId 的可能性。

关于javascript - BreezeJS - 链接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com