KnockoutJS复制数据开销 [英] KnockoutJS duplicating data overhead

查看:68
本文介绍了KnockoutJS复制数据开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我对Knockoutjs越来越感兴趣.它看起来很有希望,因为它可以像MVVM模式和WPF这样的绑定进行建模,但是每当它为非RIA Web应用程序带来有用的东西时,我确实会怀疑(而且当我说RIA时,我的意思是浏览器应用程序很复杂,比如说ERP).比向表中添加几行并隐藏其中要复杂一些)

For the past few days I'm getting more interested in Knockoutjs. It looks very promising because it models the MVVM pattern and WPF like bindings, but I do have some doubts whenever it bring something useful to non RIA web apps (and when I say RIA I mean complex in browser applications, let's say an ERP, anyway something a bit more complex than adding a few rows to a table and hiding one)

假设您有一个包含10个项目的组合框,并且您需要能够在客户端另外创建2个项目并将其保存在服务器上.

Let's say you have a combobox with 10 items, and you need to be able to create on client side another 2 items and save them on the server.

以我的方式看,您将必须创建带有Obs的viewmodel.数组中预先填充了10个项目,并在组合框内渲染了10个项目(作为10个选项元素).

The way I see it you would have to create a viewmodel with a Obs. array prefilled with the 10 items, and also render the 10 items inside the combobox (as 10 option elements).

基本上,您将有2次循环两次,使项目集合两次,并在js viewmodel和combobox(选项)中呈现项目.

Basically you would have 2 loop twice the item collection and render the items in js viewmodel and the combobox (options).

现在想像一下,如果您有30个控件,而没有具有剔除视图模型和带有预填充数据的html控件的信息,那会是负担吗?

Now imagine you would have 30 controls wouldn't having information on both knockout view model and html controls with pre-filled data be an overhead?

推荐答案

每种情况都不同,但我认为您通常不需要呈现这些组合框选项以及在Knockout视图模型中定义它们-Knockout将生成为您提供选项HTML. 我在复杂的对象图中以这种方式填充了许多组合框.当页面第一次加载时,对象图的初始状态在页面中呈现为对象文字,然后我将其传递到顶层的Knockout视图模型构造函数中,该构造函数通过映射插件创建整个结构(创建子视图模型等) ). 然后,所有的保存和更新都是通过小的离散Ajax操作并使用映射插件重新映射来完成的.

Every situation is different but I don't think you would normally need to render those combobox options as well as defining them in a Knockout view model - Knockout will build the options HTML for you. I have numerous comboboxes populated in this way in a complex object graph. When the page first loads the initial state of the object graph is rendered in the page as an object literal which I then pass into my top-level Knockout view model constructor, which creates the entire structure via the mapping plugin (creating child view models etc). Then all saving and updating is done via small discrete Ajax operations and remapping with the mapping plugin.

这篇关于KnockoutJS复制数据开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆