Emberjs 类似于 ajax 在 Helper 中请求 return
Emberjs something like ajax request return in Helper
经过多次观察和尝试,我相信没有办法从 Emberjs 帮助中的 ajax 请求中 return 一个值。 (我希望我错了。)我相信原因是整个 ansyc 回调。
这就是我的困境;
我有一个父模型 A,它有模型 B 的子模型。我想显示父模型和子模型以及一些额外信息。额外信息来自使用模型 B 的一些信息的 api 调用。我没有在模型 B 上保存这些额外信息,但我想显示它。所以基本上,我有这样的东西:
{{#each modelb in modela.modelbs}}
...
{{/each}}
我希望能够做类似的事情:
{{#each modelb in modela.modelbs}}
{{get-info modelb}}
{{/each}}
它 return 我可以从 api 电话中获得的信息。
我试过像我之前说的那样使用助手。我尝试在控制器中放置一些逻辑,但我无法隔离单个子模型来创建计算 属性。 (另外,我不确定 computerd 属性 是否会帮助我,我认为它需要一个 return 语句,这让我回到了与助手相同的问题,我认为我无法创建子关系的计算 属性。)虽然我从 api 调用中获得了我需要的额外信息,但我无法将它与我的子模型 B 相关联。
我想我在 'ember way' 中没有考虑这个问题。希望有更好的方法来解决这个问题。
如有任何帮助,我们将不胜感激。
既然你有数据,你可以做的是,当你调用 modelA 或 ModelB 时,将某种类型的 id 作为参数附加到调用函数,return call 的回调。通过回调,您可以调用一个函数来找到正确的模型来附加数据?
例如:
var objs = [
{id:1, info: "hi", extraInfo: ""},
{id:2, info: "bye", extraInfo: ""}
];
for(var i = 0; i < objs.length; i++){
getRelatedInfo("extraInfo", objs[i].id);
}
function getRelatedInfo(someParam, 1){
// call
// on success
attachInfoToObj(result, 1);
}
function attachInfoToObj(data, id){
for(var i = 0; i < objs.length; i++){
if(objs[i].id === id){
objs[i].extraInfo = data;
}
}
}
您可以在 modela 的 afterModel 挂钩中获取 modelb 的信息。示例:
App.ModelaRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('modela', params.id);
},
afterModel: function(modela) {
// Note: afterModel is called after modela is loaded
// Iterate through modelbs
modela.get('modelbs').foreach(function(modelb) {
// Get current modelb's extra information by ajax. Note that this may result in *many* ajax requests - may be undesirable
ajaxRequestForModelbInfo.done(function(response) {
// Assign extra information to modelb's extraInformation property
modelb.set('extraInformation', response);
});
});
}
});
在hbs模板文件中:
{{#each modelb in modela.modelbs}}
{{modelb.extraInformation}}
{{/each}}
这是我最后得到的结果。
在我的路线中我写:
export default Ember.Route.extend({
//model is already loaded from parent route.
setupController: function(controller, model){
var modela = model; //just for clarity with the whole modela/modelb thing.
modela.get('modelbs').forEach(function(modelb){
$.ajax({
//some ajax call using modelb's information.
}).then(function(data){
//extraInformation is not a model definted property on modelb, but I just added extraInformation as an index to modelb.
modelb['extraInformation'] = data;
controller.set('model', modela);
});
});
}
});
然后在我的模板中写:
{{#each modelb in modela.modelbs}}
{{modelb.extraInformation}}
{{/each}}
希望这对某人有所帮助。
经过多次观察和尝试,我相信没有办法从 Emberjs 帮助中的 ajax 请求中 return 一个值。 (我希望我错了。)我相信原因是整个 ansyc 回调。
这就是我的困境;
我有一个父模型 A,它有模型 B 的子模型。我想显示父模型和子模型以及一些额外信息。额外信息来自使用模型 B 的一些信息的 api 调用。我没有在模型 B 上保存这些额外信息,但我想显示它。所以基本上,我有这样的东西:
{{#each modelb in modela.modelbs}}
...
{{/each}}
我希望能够做类似的事情:
{{#each modelb in modela.modelbs}}
{{get-info modelb}}
{{/each}}
它 return 我可以从 api 电话中获得的信息。
我试过像我之前说的那样使用助手。我尝试在控制器中放置一些逻辑,但我无法隔离单个子模型来创建计算 属性。 (另外,我不确定 computerd 属性 是否会帮助我,我认为它需要一个 return 语句,这让我回到了与助手相同的问题,我认为我无法创建子关系的计算 属性。)虽然我从 api 调用中获得了我需要的额外信息,但我无法将它与我的子模型 B 相关联。
我想我在 'ember way' 中没有考虑这个问题。希望有更好的方法来解决这个问题。
如有任何帮助,我们将不胜感激。
既然你有数据,你可以做的是,当你调用 modelA 或 ModelB 时,将某种类型的 id 作为参数附加到调用函数,return call 的回调。通过回调,您可以调用一个函数来找到正确的模型来附加数据?
例如:
var objs = [
{id:1, info: "hi", extraInfo: ""},
{id:2, info: "bye", extraInfo: ""}
];
for(var i = 0; i < objs.length; i++){
getRelatedInfo("extraInfo", objs[i].id);
}
function getRelatedInfo(someParam, 1){
// call
// on success
attachInfoToObj(result, 1);
}
function attachInfoToObj(data, id){
for(var i = 0; i < objs.length; i++){
if(objs[i].id === id){
objs[i].extraInfo = data;
}
}
}
您可以在 modela 的 afterModel 挂钩中获取 modelb 的信息。示例:
App.ModelaRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('modela', params.id);
},
afterModel: function(modela) {
// Note: afterModel is called after modela is loaded
// Iterate through modelbs
modela.get('modelbs').foreach(function(modelb) {
// Get current modelb's extra information by ajax. Note that this may result in *many* ajax requests - may be undesirable
ajaxRequestForModelbInfo.done(function(response) {
// Assign extra information to modelb's extraInformation property
modelb.set('extraInformation', response);
});
});
}
});
在hbs模板文件中:
{{#each modelb in modela.modelbs}}
{{modelb.extraInformation}}
{{/each}}
这是我最后得到的结果。
在我的路线中我写:
export default Ember.Route.extend({
//model is already loaded from parent route.
setupController: function(controller, model){
var modela = model; //just for clarity with the whole modela/modelb thing.
modela.get('modelbs').forEach(function(modelb){
$.ajax({
//some ajax call using modelb's information.
}).then(function(data){
//extraInformation is not a model definted property on modelb, but I just added extraInformation as an index to modelb.
modelb['extraInformation'] = data;
controller.set('model', modela);
});
});
}
});
然后在我的模板中写:
{{#each modelb in modela.modelbs}}
{{modelb.extraInformation}}
{{/each}}
希望这对某人有所帮助。