如何将 HTML5 拖放与 KnockoutJS 结合使用?
我似乎无法绑定到 html5 拖放事件。
这是模板的示例:
<script id="tabsTemplate" type="text/html">
<div class="dropzone" for="tab"
data-bind="event:{dragover: function(event){event.preventDefault();},
dragenter: function(event){event.target.addClass('dragover'); event.preventDefault();},
dragleave: function(event){event.target.removeClass('dragover'); event.preventDefault();}}
drop: function(event){console.log('blahblah!')}"></div>
<h1 class="tab" draggable="true"
data-bind="attr: {selected: $data.name === $item.selected()},
click: function(){$item.selected($data.name)},
event:{ dragstart: function(event){console.log('blah!!')},
dragend: function(event){document.getElementsByClassName('dragover')[0].removeClass('dragover')}}">
${name}
<img src="icons/close-black.png" class="close button" role="button"
data-bind="click: function(e){$item.close($data)}">
</h1>
</script>
我所拥有的应该按预期工作......只要我将它们设为正常的内联,它就可以。但是,其他绑定就不起作用了!
我收到此错误消息:
未捕获的语法错误:意外的标记“||” jquery-tmpl.js:10
这是怎么回事?我做错了什么吗?
I can't seem to bind to html5 drag and drop events.
Here's an example of from a template:
<script id="tabsTemplate" type="text/html">
<div class="dropzone" for="tab"
data-bind="event:{dragover: function(event){event.preventDefault();},
dragenter: function(event){event.target.addClass('dragover'); event.preventDefault();},
dragleave: function(event){event.target.removeClass('dragover'); event.preventDefault();}}
drop: function(event){console.log('blahblah!')}"></div>
<h1 class="tab" draggable="true"
data-bind="attr: {selected: $data.name === $item.selected()},
click: function(){$item.selected($data.name)},
event:{ dragstart: function(event){console.log('blah!!')},
dragend: function(event){document.getElementsByClassName('dragover')[0].removeClass('dragover')}}">
${name}
<img src="icons/close-black.png" class="close button" role="button"
data-bind="click: function(e){$item.close($data)}">
</h1>
</script>
What I have should work as expected... and it does as long as I make them normal inline ones. However, then the other bindings don't work!
I am getting this error message:
Uncaught SyntaxError: Unexpected token '||' jquery-tmpl.js:10
What's going on here? Is there something I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我已经解决了。看来我错过了文档中所说的,在淘汰赛中,默认情况下它会使所有事件阻止默认/返回 false。所以我所要做的就是让我的 Dragstart 处理程序返回 true,现在它可以工作了。唷!!
OK, I have worked it out. It seems I missed in the documentation where it said that in knockout, by default it makes all events prevent default / return false. So all I had to do was make my dragstart handler return true, and now it works. Phew!!
对于那些(像我一样)需要 SSCCE 工作的人;该解决方案遵循 [cybermotron] 建议,还修复了处理程序需要参数数据和事件的问题。
http://jsfiddle.net/marrok/m63aJ/
HTML
淘汰赛
For those (like me) who need a SSCCE working; the solution follow's [cybermotron] suggestion, also fixes an issue where handlers expect prarameters data and event.
http://jsfiddle.net/marrok/m63aJ/
HTML
Knockout
您可能遇到与此处,尽管它指的是嵌套模板:
警告
如果您将 templateOptions 从嵌套模板传递到模板绑定(因此,从模板内指定模板绑定),请特别注意您的语法。如果您的绑定如下所示,您将遇到一个问题:
jQuery 模板插件会被绑定末尾的 }} 混淆,因为这是其语法的一部分。在大括号之间添加一个空格就可以了。希望这可以防止某人遭受一些不必要的挫败感。
You might have the same problem as mentioned here, although it refers to nested templates:
Warning
If you are passing templateOptions to the template binding from a nested template (so, specifying a template binding from within a template), then pay special attention to your syntax. You will encounter a problem, if your binding looks like this:
The jQuery Templates plugin gets confused by the }} at the end of your binding, since that is part of its syntax. Adding a space between your braces will work fine. Hopefully this prevents someone from a little unnecessary frustration.