简体   繁体   English

backbone.js - 模型/视图如何链接到DOM元素?

[英]backbone.js - How are models/views linked to DOM element?

I'm just playing around with backbone.js and some jQuery magic to prepare for some upcoming projects. 我只是在玩backbone.js和一些jQuery魔法来为即将到来的项目做准备。

One test case contains a table whose rows are rendered by a backbone view. 一个测试用例包含一个表,其行由骨干视图呈现。 They get perfectly re-rendered on value change. 它们在价值变化时得到了完美的重新渲染。 Afterwards the whole table is sorted by an jQuery plugin (Animated Table Sort), rows move to new positions. 然后整个表按jQuery插件(Animated Table Sort)排序,行移动到新位置。 In fact, this process works once, but the next time, rows appear twice, everything ends up in chaos. 事实上,这个过程只运行一次,但下一次,行出现两次,一切都以混乱结束。

Is it possible, that the link between DOM element and backbone view can't handle such an change? DOM元素和骨干视图之间的链接是否有可能无法处理这种变化? Are there any workarounds? 有没有解决方法?

When you're developing with a Model/View framework like backbone.js or knockout.js, I find that you need to re-arrange your thinking and implementations to make changes to what is diplayed (like sorting) to the Model, and not allow them to happen in the view (like using a jquery plugin). 当您使用像backbone.js或knockout.js这样的模型/视图框架进行开发时,我发现您需要重新安排您的思考和实现,以便对模型的显示(如排序)进行更改,而不是允许它们在视图中发生(比如使用jquery插件)。

If you do end up using a view-side script to do something fancy (animations are a good example), then it is up to you to make sure the model is updated correctly, either by disabling or extending the binding. 如果你最终使用视图端脚本做一些奇特的事情(动画就是一个很好的例子),那么你可以通过禁用或扩展绑定来确保模型正确更新。

Also note that according to the documentation , that animated sort plugin removes your table rows from the DOM, adds them to new DIVs, animates them, removes them from the DIVs, and restores them to the table. 另请注意,根据文档 ,动画排序插件会从DOM中删除表行,将它们添加到新DIV中,为它们设置动画,将它们从DIV中删除,然后将它们还原到表中。 I'm wondering if after this is all done, backbone has lost track of those TDs, and when it re-renders after the change, it's just adding a new set since the last set is 'gone'. 我想知道在完成这一切后,骨干网已经失去了对这些TD的追踪,并且当它在更改后重新渲染时,它只是添加了一个新的集合,因为最后一个集合已经“消失”。

Thanks for your answers. 谢谢你的回答。 Indeed, the table sorter does a lot that makes it difficult fpr backbone to maintain bindings. 实际上,表格分类器做了很多事情,这使得fpr骨干很难维护绑定。 I've switched over to the great Quicksand plugin which uses a hidden list to animate changes in another (visible) list. 我已经切换到了很棒的Quicksand插件 ,该插件使用隐藏列表来动画另一个(可见)列表中的更改。 Fits better to backbone.js. 更适合backbone.js。

Your collection maintains an order for your models, and therefor your corresponding views. 您的收藏品会维护您的模型订单,以及相应的视图。 If an outside force (like a jQuery table sorting plugin) modifies the order of the views, this change is not inherently reflected in the Backbone collection, so things are quickly out of sync. 如果外部力量(如jQuery表排序插件)修改了视图的顺序,则此更改不会反映在Backbone集合中,因此很快就会失去同步。

Also, if the table sorter clones elements and removes the original, Backbone would likely lose track of the views and end up recreating them. 此外,如果表格分类器克隆元素并删除原始元素,Backbone可能会丢失视图跟踪并最终重新创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM