【发布时间】:2016-05-24 21:13:05
【问题描述】:
我们有一个页面,“parent”,它通过在 parent.html 中称为“child”的 view-model.ref 引用一个模板。我们通过单击父页面上的项目来更改此子模板的数据,该父页面使用OpenDetailsDiv 调用子函数。假设我为此事件使用了一个按钮,如下所示:
parent.html
<child view-model.ref="clientusertasks"></child>
<input type="button" value="Click Me" click.trigger="OpenDetailsDiv" />
通过这种方式,我们可以从父视图模型中调用“子”视图模型上的函数,如下所示:
parent.js
import { inject } from 'aurelia-framework';
import { HttpClient } from 'aurelia-fetch-client';
import 'fetch';
import AuthService from 'AuthService';
import { BpoClientUserTasks } from './bpo-client-user-tasks';
@inject(HttpClient, AuthService, BpoClientUserTasks)
export class Parent {
smallDivObj = {};
freq = '';
period = '';
filterVal = '';
client = '';
constructor(http, AuthService, BpoClientUserTasks) {
http.configure(config => {
config
.withBaseUrl("WebServices.asmx/")
.withDefaults({
headers: {
'Accept': 'application/json'
}
});
});
this.http = http;
this.auth = AuthService;
this.clientusertasks = BpoClientUserTasks;
}
OpenDetailsDiv(myObject) {
this.clientusertasks.CallBPOClientUserService(this.freq, this.period, this.filterVal, myObject.TrueClient, myObject.Client);
}
}
到目前为止一切顺利。 “子”视图模型具有此功能 CallBPOClientUserService,如下所示:
child.js
import { inject } from 'aurelia-framework';
import { HttpClient } from 'aurelia-fetch-client';
import 'fetch';
import AuthService from 'AuthService';
@inject(HttpClient, AuthService)
export class Child {
smallDivObj = {};
constructor(http, AuthService) {
http.configure(config => {
config
.withBaseUrl("WebServices.asmx/")
.withDefaults({
headers: {
'Accept': 'application/json'
}
});
});
this.http = http;
this.auth = AuthService;
}
attached() {
}
CallBPOClientUserService(freq, period, filterVal, client, displayClient) {
$('#TasksByClientByUserDiv').addClass("fade");
this.freq = freq;
this.period = period;
this.filterVal = filterVal;
this.client = client;
var mymethod = {
method: 'post',
body: JSON.stringify({
"session": this.auth.session,
"Client": client,
"FreqFilter": freq,
"FilterVal": filterVal
}),
headers: {
'content-type': 'application/json'
}
};
//alert(JSON.stringify(mymethod));
this.http.fetch('GetBPOTasksByClientByUserDiv', mymethod)
.then(response => response.json())
.then(info => {
this.tasksByClientByUser = JSON.parse(info.d);
//setTimeout("$('#TasksByClientByUserTable').tablesorter();", 100);
});
}
}
请注意,在函数CallBPOClientUserService 中,我们希望在渲染DOM 之后调用tablesorter 排序函数来对视图中的表进行排序。
通常我会在视图模型的“附加”组件生命周期中调用此函数。但是您可以看到,我们填充此视图的方式来自“父”页面的 view-model.ref,它使“子”的“附加”组件在这种情况下无用(毕竟它只被调用一次加载父级时)。
所以我的问题:
我是否可以利用类似attached 的组件来调用此tablesorter 函数?
我有一个廉价的解决方法,我可以使用我在函数中注释的setTimeout,但我宁愿在 Aurelia 事件中正确执行此操作并保证 DOM 已完成。
【问题讨论】:
-
您可以尝试将您的函数推送到 microTaskQueue,如本答案stackoverflow.com/questions/35587033/…中所述
-
是的,我喜欢这样。在调用 jquery 函数之前,我们还考虑使用可绑定事件来结束 table 上的 repeat.for。
-
试图用
bind()代替attached()?
标签: aurelia