木偶.js - 我可以检测到追加吗?
Marionette.js - can I detect onAppend?
我有一个愚蠢的问题,我唯一的解决方案是一个草率的黑客,现在给我带来了其他问题。
看我的小提琴,
或在此处阅读代码:
.HTML:
<input id='1' value='input1' />
<template id='template1'>
<input id='2' value='input2' />
</template>
JS - 项目视图声明:
// Declare an ItemView, a simple input template.
var Input2 = Marionette.ItemView.extend({
template: '#template1',
onRender: function () {
console.log('hi');
},
ui: { input2: '#2' },
onRender: function () {
var self = this;
// Despite not being in the DOM yet, you can reference
// the input, through the 'this' command, as the
// input is a logical child of the ItemView.
this.ui.input2.val('this works');
// However, you can not call focus(), as it
// must be part of the DOM.
this.ui.input2.focus();
// So, I have had to resort to this hack, which
// TOTALLY SUCKS.
setTimeout(function(){
self.ui.input2.focus();
self.ui.input2.val('Now it focused. Dammit');
}, 1000)
},
})
JS - 控制器
// To start, we focus input 1. This works.
$('#1').focus();
// Now, we make input 2.
var input2 = new Input2();
// Now we 1. render, (2. onRender is called), 3. append it to the DOM.
$(document.body).append(input2.render().el);
正如上面看到的,我的问题是我无法在渲染 (onRender
) 后使 View 调用聚焦于自身,因为它尚未附加到 DOM 中。据我所知,没有其他事件调用,例如 onAppend
,可以让我检测到它何时实际附加到 DOM 中。
我不想从项目视图外部调用焦点。为了我的目的,它必须从内部完成。
有什么好主意吗?
更新
事实证明,onShow()
在 Marionette.js 中的所有 DOM 追加上都被调用,无论是 CollectionView、CompositeView 还是 Region,它都不在文档中!
谢谢一百万,卢卡兹菲泽。
解决方案是在Marionette.Region
中呈现您的ItemView
。这样,一旦视图插入到 DOM 中,就会在视图上调用 onShow
方法。
例:
.HTML
<input id='1' value='input1' />
<div id="inputRegion"></div>
<template id='template1'>
<input id='2' value='input2' />
</template>
JS项目视图
(...)
onShow: function () {
this.ui.input2.val('this works');
this.ui.input2.focus();
},
(...)
JS控制器
$('#1').focus();
var inputRegion = new Backbone.Marionette.Region({
el: "#inputRegion"
});
var input2 = new Input2();
inputRegion.show(input2);
木偶文档中的更多信息:https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.region.md#region-events-and-callbacks
好吧,我设法通过扩展木偶.js解决了它,但如果其他人有更好的主意,不涉及扩展库,我会欣然接受并给你买一个甜甜圈。
// After studying Marionette.js' annotated source code,
// I found these three functions are the only places
// where a view is appended after rendering. Extending
// these by adding an onAppend call to the end of
// each lets me focus and do other DOM manipulation in
// the ItemView or Region, once I am certain it is in
// the DOM.
_.extend(Marionette.CollectionView.prototype, {
appendHtml: function(collectionView, itemView, index){
collectionView.$el.append(itemView.el);
if (itemView.onAppend) { itemView.onAppend() }
},
});
_.extend(Marionette.CompositeView.prototype, {
appendHtml: function(cv, iv, index){
var $container = this.getItemViewContainer(cv);
$container.append(iv.el);
if (itemView.onAppend) { itemView.onAppend() }
},
});
_.extend(Marionette.Region.prototype, {
open: function(view){
this.$el.empty().append(view.el);
if (view.onAppend) { view.onAppend() }
},
});
相关文章:
- 如何检测是否有溢出
- 表追加而不附加最后一个元素
- 如何检测用于WebGL的专用或集成显卡
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 如何使用密码检测网络中的状态连接
- 使用JavaScript检测Flash
- 在不阻止默认行为的情况下检测IE10中的缩放
- 检测个位数整数时正在转换毫秒
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 动态插入的表:JQuery未检测到最后一行
- 检测页面上某个元素中选择(突出显示)或单击的内容
- jQuery检测浏览器是否支持Zoom
- 检测iframe是否跨域的愚蠢方法
- 检测图像分辨率
- 如何检测第三方广告服务器请求
- jQuery[button.class]未检测到用按钮追加行
- 如何检测 html 元素是否可以追加子节点
- 木偶.js - 我可以检测到追加吗?
- 是否有可能监视或以某种方式检测子dom节点何时追加到父节点