简体   繁体   English

View的骨干变换模型

[英]Backbone change model of View

I am fairly new to Backbone and have the following question: 我对Backbone相当新,并提出以下问题:

I have a collection of models. 我有一系列模特。

I have a collection view displaying tabs (with a view for each model in the collection). 我有一个显示选项卡的集合视图(包含集合中每个模型的视图)。

I have a view for a model (for the content). 我有一个模型的视图(内容)。

I have a router with routes. 我有一个路由器的路由器。

What I am trying to achieve is a functionality like http://jqueryui.com/demos/tabs/ 我想要实现的是像http://jqueryui.com/demos/tabs/这样的功能

I click on a tab (model of collection) and then want to pass the model to the content view maybe change it and reflect the changes in the collection. 我单击选项卡(集合模型),然后想要将模型传递到内容视图,可能会更改它并反映集合中的更改。

I came up with four solutions: 我想出了四个解决方案:

In the router: 在路由器中:

'switchCommunity': function(id) {
        // (a) set new model attributes
        this.view.community.model.set(communities.get(id));

        // (b) replace model
        this.view.community.model = communities.get(id);

        // (c) a custom function of the view changes its model
        this.view.community.changeModel(communities.get(id));

        // (d) a new view
        this.view.community = new View({
            model: communities.get(id)
        })
}

The problem here is 这里的问题是

  • (a) does not reflect changes to the model in the collection (a)不反映集合中模型的变化

  • (b) does not trigger (change) events, because the bind in the initialize function of the view never triggers, because it is a completly new model (b)不触发(改变)事件,因为视图的初始化函数中的绑定永远不会触发,因为它是一个完全新的模型

  • (c) seems like a hack to me (c)对我来说似乎是个黑客

  • (d) everytime i click on a tab a new view is created (is this a performance issue?) (d)每次我点击选项卡时都会创建一个新视图(这是性能问题吗?)

What is the best pratice here? 这里最好的实践是什么?

One of your solution is close to be okay :D 你的解决方案之一就好了:D

Here is what you want: 这是你想要的:

this.view.community.model.set(communities.get(id).toJSON());

And this will trigger model.on("change") as well. 这也将触发model.on(“更改”)。

Why do you think (c) is a hack? 为什么你认为(c)是黑客? It seems like a good place to encapsulate the unbinding of the old model, and connecting up the new one. 这似乎是一个封装旧模型解除绑定并连接新模型的好地方。

The Backbone.Marionette plugin provides a streamlined solution for your problem. Backbone.Marionette插件为您的问题提供了简化的解决方案。

It provides functionality for Application Initialization, View Management, and Event Aggregation. 它提供了应用程序初始化,视图管理和事件聚合的功能。

In essence, it takes the pain out of hiding and showing multiple views. 从本质上讲,它可以消除隐藏和显示多个视图的痛苦。

You can read this blog post to learn more about it. 您可以阅读此博客文章以了解有关它的更多信息。

The short answer is you should use d. 简短的回答是你应该使用d。 Yes is isn't performant but unless you notice slowdown in the user interface you shouldn't worry too much. 是的并不是高效的,但除非你注意到用户界面的减速,否则你不必过于担心。 You should code something that 1. always works 2. doesn't take long to code so you can move on to coding other more important features. 您应该编写1.总是有效的代码.2。代码编写时间不长,因此您可以继续编写其他更重要的功能。

If/when you need more performance then you can take the extra time to do c. 如果/当您需要更多性能时,您可以花费额外的时间来做c。 To be the most performant you shouldn't destroy and re-render templates. 要成为最高效的人,不应破坏和重新渲染模板。 You should use jquery to manually find the elements on the DOM and replace them with the new model. 您应该使用jquery手动查找DOM上的元素并将其替换为新模型。 When you call: 你打电话的时候:

view.$el = _.template(string, model); 

It's very little code but lots of work for the browser. 这是很少的代码,但浏览器的工作量很大。 Replacing the DOM with a new model is much more performant. 用新模型替换DOM的性能要高得多。

If you need to be more performant you can use object pooling. 如果您需要更高性能,可以使用对象池。 I've been working on a PerfView for backbone that implements a lot of optimizations. 我一直在研究PerfView for backbone,它实现了很多优化。 https://github.com/puppybits/BackboneJS-PerfView There's comments in the code with a lot of best practices to maintain the best browser performance. https://github.com/puppybits/BackboneJS-PerfView代码中有一些注释,其中包含许多保持最佳浏览器性能的最佳实践。

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

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