我是不是总是使用 Knockout 映射插件来做我的视图模型而过度使用它? [英] Am I overusing the Knockout mapping plugin by always using it to do my viewmodel?

查看:25
本文介绍了我是不是总是使用 Knockout 映射插件来做我的视图模型而过度使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习 Knockout 的正确用法,而且我发现自己在设置视图模型时很快就不再输入 ko.observable,而只是定义一个对象文字并传递它通过类似

I'm still learning the proper usage of Knockout and I've found myself quickly getting away from ever typing ko.observable when setting up my viewmodel and instead just defining an object literal and passing it through the mapping plugin with something like

var viewModel = ko.mapping.fromJS(data);

或者至少,像这样将我的所有数据填充到 viewModel 上的一个属性中

or at the very least, something along the lines of stuffing all of my data into an attribute on the viewModel like so

var viewModel = { 
    ... events etc ... , 
    "data": ko.mapping.fromJS(data)
}

老实说,我这样做的主要原因是为了避免重复输入 ko.observableko.observableArray.我只是想弄清楚这是否是一个好方法,以及将特定的 var x = ko.observable() 声明放在一起是否有任何缺点.此外,我是在加载时完成这一切,而不是响应任何 ajax 调用等,据我所知,这是映射插件的设计目的.

To be honest, the main reason I've been doing this is to get around having to type ko.observable and ko.observableArray repetitively. I'm just trying to figure out if this is a good approach and if there are any downsides to dropping the specific var x = ko.observable() declaration all together. Also, I'm doing this all on load, not in response to any ajax call etc, which from what I can tell, is what the mapping plugin was designed for.

在你使用knockout的工作中,你是否仍然手动一个一个地声明observable,或者你是否使用了我使用的mapping.fromJS方法?像这样频繁地使用映射插件有什么具体的缺点吗?

In your work with knockout, do you still declare the observables manually, one by one, or have you gone with the mapping.fromJS method that I use? Are there any specific downsides to using the mapping plugin so frequently like this?

在本文中,史蒂夫设置通过做他的viewModel

In this article, Steve sets up his viewModel by doing

var initialData = [ { ... } , { ... } ]; // json from the serializer
var viewModel = {
    gifts : ko.observableArray(initialData)
};

通常,我也会在这种情况下使用 ko.mapping.fromJS,特别是为了确保数组中的对象也变成了可观察对象.看看他的所作所为,我的方法似乎有点矫枉过正,并增加了一些不必要的开销.

Normally, I'd just use ko.mapping.fromJS for this situation as well, specifically to make sure the objects within the array are turned into observables as well. Looking at what he did, my approach seems like its overkill and adds a bit of unnecessary overhead.

推荐答案

我对你的建议与我刚刚在 https://stackoverflow.com/questions/7499133/mapping-deeply-hierarchical-objects-to-custom-classes-using-knockout-映射插件.

My suggestion to you would the same another questioned I just answered at https://stackoverflow.com/questions/7499133/mapping-deeply-hierarchical-objects-to-custom-classes-using-knockout-mapping-plug.

你使用映射插件的理由是合理的,也是我使用的.为什么要输入比您需要的更多的代码?

Your reasoning for using mapping plug-in is reasonable and the one that I use. Why type more code than you have to?

根据我的淘汰赛经验(全部 4 个月),我发现我手动执行的操作越少,而是让淘汰赛例程完成它们的工作,我的应用程序似乎运行得越好.我的建议是先尝试最简单的方法.如果它不能满足您的需求,请查看简单的方法是如何做它的事情",并确定必须更改哪些内容才能满足您的需求.

In my experience with knockout (all of 4 months), I've found that the less I do manually and let the knockout routines do their thing, the better my apps seem to run. My suggestion is try the simplest approach first. If it doesn't meet your needs, look at how the simple approach is doing it's "thing" and determine what has to change to meet your needs.

这篇关于我是不是总是使用 Knockout 映射插件来做我的视图模型而过度使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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