从事件中重新触发事件
Re-trigger event from event
这是一个搜索框模块(经过大量清理以满足老板的要求)。除了 more
事件外,一切正常。我得到了结果,它们出现了,"load more" div 出现了。
单击 more
后,我希望 search
事件重新运行。在 more
中,我做了一些简单的会话计算,我需要将这些计算传递给 search
事件,该事件有效。 click()
没有。
我试过将 more
事件移到 search
事件之外,但没有成功。
This fiddle seems to work, albeit with the node ID instead.
define([
'dojo/dom',
'dojo/dom-construct',
'dojo/on',
'dojo/query'
'dojo/domReady!'
], function(
dom,
domConstruct,
on,
query
) {
var search = {
display: function(){
sb = '<div id="searchWrapper" class="searchWrapper"><h3>Search</h3>
<input id="searchString" type="text" />';
sb += '<h3>Search</h3><select id="sBy">';
sb += '<option value="0">Choose...</option>';
sb += '<option value="f">Foo</option>';
sb += '<option value="b">Bar</option>';
sb += '<option value="fb">Foo & Bar</option>';
sb += '</select>';
sb += '<div class="searchBtn">
<button type="button" id="search">Search</button></div>';
sb += '</div>';
domConstruct.place(sb, dom.byId('body'), 1);
on(dom.byId('search'), 'click', function(e){
var r = thisIsWhereDataIsRetrieved(params, whatever);
// r is the response from the db call
if(typeof r === 'object'){
var sr = 'here are the results!';
for (var result in r) {
sr += 'this particular result';
}
if(r.length === 50) {
sr += '<div id="more" class="more">Load More</div>';
}
domConstruct.place(sr, dom.byId('searchWrapper'), 'last');
}
on(query('.more'), 'click', function(e){
// get next results having done stuff
// to track the state of the search, which works
dom.byId('search').click(); <<<<< not working
});
});
}
}
return search;
});
编辑:我通过在 more
事件中重新调用 thisIsWhereDataIsRetrieved
并将下一个结果附加到 more
节点之前来解决这个问题,但它仍然看起来好像'最好做我做不到的事!
EDIT2:添加了 more
节点!
您需要使用匿名函数以外的东西。
例如,像这样:
(请注意,您可能需要比实际方法更好的构造,如果您没有正确清理 dom,使用查询可能会产生副作用。我鼓励您编写适当的小部件并使用附加点)
define([
'dojo/dom',
'dojo/dom-construct',
'dojo/on',
'dojo/query'
'dojo/domReady!'
], function(
dom,
domConstruct,
on,
query
) {
var search = {
display: function() {
sb = '<div id="searchWrapper" class="searchWrapper"><h3>Search</h3>
<input id="searchString" type="text" />';
sb += '<h3>Search</h3><select id="sBy">';
sb += '<option value="0">Choose...</option>';
sb += '<option value="f">Foo</option>';
sb += '<option value="b">Bar</option>';
sb += '<option value="fb">Foo & Bar</option>';
sb += '</select>';
sb += '<div class="searchBtn">
<button type="button" id="search">Search</button></div>';
sb += '</div>';
domConstruct.place(sb, dom.byId('body'), 1);
var onSearchClick = function(e) {
var r = thisIsWhereDataIsRetrieved(params, whatever);
// r is the response from the db call
if (typeof r === 'object') {
var sr = 'here are the results!';
for (var result in r) {
sr += 'this particular result';
}
if (r.length === 50) {
sr += '<div id="more" class="more">Load More</div>';
}
domConstruct.place(sr, dom.byId('searchWrapper'), 'last');
}
on(query('.more'), 'click', function(e) {
// get next results having done stuff
// to track the state of the search, which works
onSearchClick();
});
}
on(dom.byId('search'), 'click', function(e) {
// get next results having done stuff
// to track the state of the search, which works
onSearchClick();
});
}
}
return search;
});
这是一个搜索框模块(经过大量清理以满足老板的要求)。除了 more
事件外,一切正常。我得到了结果,它们出现了,"load more" div 出现了。
单击 more
后,我希望 search
事件重新运行。在 more
中,我做了一些简单的会话计算,我需要将这些计算传递给 search
事件,该事件有效。 click()
没有。
我试过将 more
事件移到 search
事件之外,但没有成功。
This fiddle seems to work, albeit with the node ID instead.
define([
'dojo/dom',
'dojo/dom-construct',
'dojo/on',
'dojo/query'
'dojo/domReady!'
], function(
dom,
domConstruct,
on,
query
) {
var search = {
display: function(){
sb = '<div id="searchWrapper" class="searchWrapper"><h3>Search</h3>
<input id="searchString" type="text" />';
sb += '<h3>Search</h3><select id="sBy">';
sb += '<option value="0">Choose...</option>';
sb += '<option value="f">Foo</option>';
sb += '<option value="b">Bar</option>';
sb += '<option value="fb">Foo & Bar</option>';
sb += '</select>';
sb += '<div class="searchBtn">
<button type="button" id="search">Search</button></div>';
sb += '</div>';
domConstruct.place(sb, dom.byId('body'), 1);
on(dom.byId('search'), 'click', function(e){
var r = thisIsWhereDataIsRetrieved(params, whatever);
// r is the response from the db call
if(typeof r === 'object'){
var sr = 'here are the results!';
for (var result in r) {
sr += 'this particular result';
}
if(r.length === 50) {
sr += '<div id="more" class="more">Load More</div>';
}
domConstruct.place(sr, dom.byId('searchWrapper'), 'last');
}
on(query('.more'), 'click', function(e){
// get next results having done stuff
// to track the state of the search, which works
dom.byId('search').click(); <<<<< not working
});
});
}
}
return search;
});
编辑:我通过在 more
事件中重新调用 thisIsWhereDataIsRetrieved
并将下一个结果附加到 more
节点之前来解决这个问题,但它仍然看起来好像'最好做我做不到的事!
EDIT2:添加了 more
节点!
您需要使用匿名函数以外的东西。
例如,像这样:
(请注意,您可能需要比实际方法更好的构造,如果您没有正确清理 dom,使用查询可能会产生副作用。我鼓励您编写适当的小部件并使用附加点)
define([
'dojo/dom',
'dojo/dom-construct',
'dojo/on',
'dojo/query'
'dojo/domReady!'
], function(
dom,
domConstruct,
on,
query
) {
var search = {
display: function() {
sb = '<div id="searchWrapper" class="searchWrapper"><h3>Search</h3>
<input id="searchString" type="text" />';
sb += '<h3>Search</h3><select id="sBy">';
sb += '<option value="0">Choose...</option>';
sb += '<option value="f">Foo</option>';
sb += '<option value="b">Bar</option>';
sb += '<option value="fb">Foo & Bar</option>';
sb += '</select>';
sb += '<div class="searchBtn">
<button type="button" id="search">Search</button></div>';
sb += '</div>';
domConstruct.place(sb, dom.byId('body'), 1);
var onSearchClick = function(e) {
var r = thisIsWhereDataIsRetrieved(params, whatever);
// r is the response from the db call
if (typeof r === 'object') {
var sr = 'here are the results!';
for (var result in r) {
sr += 'this particular result';
}
if (r.length === 50) {
sr += '<div id="more" class="more">Load More</div>';
}
domConstruct.place(sr, dom.byId('searchWrapper'), 'last');
}
on(query('.more'), 'click', function(e) {
// get next results having done stuff
// to track the state of the search, which works
onSearchClick();
});
}
on(dom.byId('search'), 'click', function(e) {
// get next results having done stuff
// to track the state of the search, which works
onSearchClick();
});
}
}
return search;
});