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

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

问题描述

我还在学习Knockout的正确使用方法,我发现自己在设置我的viewmodel时很快就输入了 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.observable ko.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?

编辑:

< a href =http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/>在本文中,史蒂夫设置了他的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-mapping-plug。

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映射插件总是用它来做我的viewmodel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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