有没有办法有条件“添加” backbone.js 中的事件?
我正在为我的网络应用程序使用backbone.js,到目前为止它运行得还不错。问题是有时我需要将项目添加到集合中并让它们转到页面顶部,而其他时候则需要转到底部。有时我需要它们来制作动画,有时则不需要。有没有办法用backbone.js干净地做到这一点?将参数传递给“add”事件将是一个好方法(例如“prepend:true”),但这似乎不是一个选项......
谢谢!
I'm using backbone.js for my web app and it works semi-well so far. The problem is that sometimes I need to add items to the collection and have them go to the top of the page, and other times to the bottom. Sometimes I need them to animate, sometimes not. Is there a way to do this cleanly with backbone.js? Passing arguments to the "add" event would be a good way (for example "prepend: true"), but this doesn't seem to be an option...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扩展 Backbone.Collection 并覆盖 add 函数:
唯一要记住的是,如果您进行前置、追加,您可能需要完全复制 Backbone.Collection.add 并使用它。
您还可以依赖集合排序。将位置和动画属性添加到模型中,对位置进行排序并侦听集合上的“添加”事件以正确设置动画。
Extend Backbone.Collection and override the add function:
Only thing to keep in mind is that if you prepend, append you might want to copy Backbone.Collection.add altogether and play with it.
You could also rely on the collection sorting. Add your position and animation attribute to the model, sort on the position and listen to "add" events on the collection to animate it correctly.