56

ko.observable我仍在学习 Knockout 的正确用法,我发现自己在设置我的视图模型时很快就摆脱了打字,而只是定义一个对象文字并将其通过映射插件传递给类似的东西

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

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

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

老实说,我这样做的主要原因是为了避免不得不ko.observable重复ko.observableArray打字。我只是想弄清楚这是否是一种好方法,以及将特定var x = ko.observable()声明全部删除是否有任何缺点。此外,我在加载时执行此操作,而不是响应任何 ajax 调用等,据我所知,这就是映射插件的设计目的。

在您的淘汰赛工作中,您是否仍然手动、一一声明可观察对象,或者您是否使用了我使用的 mapping.fromJS 方法?像这样频繁使用映射插件有什么具体的缺点吗?

编辑:

具体例子

在本文中,Steve 通过以下方式设置了他的 viewModel

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

通常,我也会ko.mapping.fromJS用于这种情况,特别是要确保数组中的对象也被转换为可观察对象。看看他做了什么,我的方法似乎有点矫枉过正,并增加了一些不必要的开销。

4

4 回答 4

59

在使用 Knockout 一段时间后,我注意到映射插件有一些额外的选项,可以让您对映射过程进行更精细的控制。

控制生成的属性的类型和数量

有几种方法可以实现这一点,我将介绍一些方法,但最终结果是您最终会从映射插件中得到更轻的结果,因为一切都是不可观察的。

基本上,您将所有您认为不会改变的东西都保留为正常属性,并且仅从您想要观察的特定项目中制作可观察对象。

省略mapping某些属性

您可以通过指定诸如ignoreor之类的内容,使映射插件从最终结果中完全省略属性include。这两者都完成了同样的事情,只是以相反的方式。

注意:示例来自knockout.js 映射插件文档,注释由我添加

映射插件参数:include

以下代码段将省略源对象中的所有属性,而不是通过include参数传入的属性。

// specify the specific properties to include as observables in the end result 
var mapping = {
    // only include these two properties
    'include': ["propertyToInclude", "alsoIncludeThis"]
}

// viewModel will now only contain the two properties listed above, 
//    and they will be observable
var viewModel = ko.mapping.fromJS(data, mapping);

映射插件参数:ignore

如果您只想从源对象中省略某些属性ignore,请使用如下所示的参数。它将从源对象中除指定属性之外的所有属性中生成可观察对象。

// specify the specific properties to omit from the result, 
//    all others will be made observable
var mapping = {
    // only ignore these two properties
    'ignore': ["propertyToIgnore", "alsoIgnoreThis"]
}

// viewModel will now omit the two properties listed above, 
//    everything else will be included and they will be an observable
var viewModel = ko.mapping.fromJS(data, mapping);

控制哪些属性是可观察的或不可观察的

如果您需要包含属性,但您认为不需要将它们设为可观察(无论出于何种原因),映射插件可以提供帮助。

映射插件参数:copy

如果您希望映射插件简单地复制普通属性而不使它们可观察,请使用此参数,如下所示。

// tell the mapping plugin to handle all other properties normally, 
//    but to simply copy this property instead of making it observable
var mapping = {
    'copy': ["propertyToCopy"]
}
var viewModel = ko.mapping.fromJS(data, mapping);

完全控制映射过程

如果您想 100% 控制在映射过程中创建的内容,包括在对象中放置闭包和订阅的能力,那么您需要使用“创建”选项。

具有计算属性的普通结果

这是一个示例,我将 ajax 调用中的数据映射到具有results属性的对象。我不想要任何可观察的东西,我只想要一个简单的生成属性,该属性将由对象上的其他简单属性组成。也许不是最引人注目的例子,但它展示了功能。

var searchMappingConfig = {
    // specific configuration for mapping the results property
    "results": {
                    // specific function to use to create the items in the results array
        "create": function (options) {
            // return a new function so we can have the proper scope/value for "this", below
            return new function () {

                // instead of mapping like we normally would: ko.mapping.fromJS(options.data, {}, this);
                // map via extend, this will just copy the properties from the returned json element to "this"
                // we'll do this for a more light weight vm since every last property will just be a plain old property instead of observable
                $.extend(this, options.data);

                // all this to add a vehicle title to each item
                this.vehicleTitle = this.Year + "<br />" + this.Make + " " + this.Model;
                }, this);
            };
        }
    }
}

订阅、关闭和映射,哦,我的

另一种情况是,如果您希望结果中有闭包和订阅。此示例太长,无法完整包含,但它适用于车辆制造商/型号层次结构。如果模型未启用,我希望给定品牌(父)的所有模型(子)都未启用,并且我希望通过订阅来完成。

// here we are specifying the way that items in the make array are created, 
//    since makes has a child array (Models), we will specify the way that 
//    items are created for that as well
var makesModelsMappingConfig = {
   // function that has the configuration for creating makes
   "create": function (options) {
      // return a new function so we can have the proper 
      //    scope/value for "this", below
      return new function () {

         // Note: we have a parent / child relationship here, makes have models. In the 
         //    UI we are selecting makes and then using that to allow the user to select 
         //    models. Because of this, there is going to be some special logic in here 
         //    so that all the child models under a given make, will automatically 
         //    unselect if the user unselects the parent make.

         // make the selected property a private variable so it can be closure'd over
         var makeIsSelected = ko.protectedComputed(false);

         // expose our property so we can bind in the UI
         this.isSelected = makeIsSelected;

         // ... misc other properties and events ...

         // now that we've described/configured how to create the makes, 
         //    describe/configure how to create the models under the makes
         ko.mapping.fromJS(options.data, {
            // specific configuration for the "Models" property                  
            "Models": {
               // function that has the configuration for creating items 
               //    under the Models property
               "create": function (model) {

                  // we'll create the isSelected as a local variable so 
                  //    that we can flip it in the subscription below, 
                  //    otherwise we wouldnt have access to flip it
                  var isSelected = ko.protectedComputed(false);

                  // subscribe to the parents "IsSelected" property so 
                  //    the models can select/unselect themselves
                  parentIsSelected.current.subscribe(function (value) {
                     // set the protected computed to the same 
                     //    value as its parent, note that this 
                     //    is just protected, not the actual value
                     isSelected(value);
                  });


                  // this object literal is what makes up each item 
                  //    in the Models observable array 
                  return {
                     // here we're returning our local variable so 
                     //    we can easily modify it in our subscription
                     "isSelected": isSelected,

                     // ... misc properties to expose 
                     //     under the item in the Model array ...

                  };
               }
            }
         }, this);
      };
   }
};

总而言之,我发现你很少需要 100% 的对象传递给插件,而且你很少需要 100% 的对象是可观察的。深入研究映射配置选项并创建各种复杂和简单的对象。这个想法是只得到你需要的一切,不多也不少。

于 2012-04-11T21:56:01.130 回答
22

我对您的建议与我刚刚在https://stackoverflow.com/questions/7499133/mapping-deeply-hierarchical-objects-to-custom-classes-using-knockout-mapping-plug 回答的另一个问题相同。

您使用映射插件的理由是合理的,并且是我使用的那个。为什么输入的代码比你必须的多?

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

于 2011-09-22T17:29:08.543 回答
4

Allen,我最近对 ​​Knockout.js 的学习经历与您的相似。我们使用来自服务器的深层层次对象图,我已经定义了显式的可实例化视图模型函数,这些函数保留了它的基本结构。

我首先将每个属性明确定义为相关视图模型上的可观察对象,但这很快就失控了。此外,切换到使用映射插件的一个主要原因是,我们必须频繁地将图的 Ajax 发布到服务器,在服务器上它与持久版本合并,然后在服务器上以这样一种方式进行验证,使得许多属性可以更改和集合被修改,并作为 Ajax 结果返回一个新实例,它必须与客户端表示重新合并。这变得非常困难,并且映射插件通过允许标识符的规范来解决添加/删除/更新以及将更新的图形重新映射到原始图形来帮助很大的时间。

它还通过使用子视图模型的“创建”选项来帮助创建原始图形。在每个视图模型构造函数中,我收到对父视图模型的引用以及用于构造子视图模型的数据,然后创建进一步的映射选项以从传入的子数据创建孙子。

我最近发现的唯一(轻微)缺点,如this question中所述,是在执行 ko.mapping.toJSON 时,它不会挂钩您可能在视图模型的原型上定义的任何 toJSON 覆盖以排除属性从序列化。正如 Ryan Niemeyer 在那篇文章中推荐的那样,我已经能够通过在取消映射中指定忽略选项来解决这个问题。

所以总而言之,我肯定会坚持使用映射插件。Knockout.js 规则。

于 2012-06-22T07:37:36.007 回答
1

A simpler but help-full add-on could be knockout-data-projections

Currently, it does not handle js to viewmodel mappings, but it handles quite well view model to JS mappings.

于 2012-04-24T10:40:01.913 回答