为什么在 Mithril 中视图不使用 m.request 重绘?
Why the view not redraw with m.request in Mithril?
我有这个例子:
controller : function() {
var responseFolder = m.prop("");
var pathDirectory = m.prop("C://");`
function clickChangeFolder(folder) {
pathDirectory(pathDirectory() + folder + "/");
responseFolder(m.request({
method : "GET",
url : "my url",
data : {root:pathDirectory()}
}));
}
return {
responseFolder: m.request({
method : "GET",
url : "http://localhost:8080/Mithril_directory/GetFolders",
data : {root:pathDirectory()}
}),
}
view : function(ctrl) {
return [
m("ul" , ctrl.responseFolder().map(function(folder) {
return [
m("li.liFolder" , {
onclick : ctrl.clickChangeFolder.bind(null, folder.name)
},
folder.name),
];
})
]}
第一次请求没问题,但是当我点击文件夹时,第二次请求没问题,但是视图没有重绘,为什么?
The basic usage pattern for m.request
returns an m.prop
getter-setter,
which is populated when the AJAX request completes.
所以你的代码会发生什么,在控制器的 return 对象上,ctrl.responseFolder
是一个 m.prop
,与之前声明的 responseFolder
变量无关.
为了在每次点击后重绘视图,需要将初始请求赋值给responseFolder
,这样就会变成getter-setter,然后return 它到视图,将在每个新请求上重新呈现。
var responseFolder = m.request({...});
...
return {
responseFolder: responseFolder,
...
};
我有这个例子:
controller : function() {
var responseFolder = m.prop("");
var pathDirectory = m.prop("C://");`
function clickChangeFolder(folder) {
pathDirectory(pathDirectory() + folder + "/");
responseFolder(m.request({
method : "GET",
url : "my url",
data : {root:pathDirectory()}
}));
}
return {
responseFolder: m.request({
method : "GET",
url : "http://localhost:8080/Mithril_directory/GetFolders",
data : {root:pathDirectory()}
}),
}
view : function(ctrl) {
return [
m("ul" , ctrl.responseFolder().map(function(folder) {
return [
m("li.liFolder" , {
onclick : ctrl.clickChangeFolder.bind(null, folder.name)
},
folder.name),
];
})
]}
第一次请求没问题,但是当我点击文件夹时,第二次请求没问题,但是视图没有重绘,为什么?
The basic usage pattern for
m.request
returns anm.prop
getter-setter, which is populated when the AJAX request completes.
所以你的代码会发生什么,在控制器的 return 对象上,ctrl.responseFolder
是一个 m.prop
,与之前声明的 responseFolder
变量无关.
为了在每次点击后重绘视图,需要将初始请求赋值给responseFolder
,这样就会变成getter-setter,然后return 它到视图,将在每个新请求上重新呈现。
var responseFolder = m.request({...});
...
return {
responseFolder: responseFolder,
...
};