简体   繁体   English

不能将KO映射扩展到多个属性

[英]Can't extend KO mapping for more than one propery

KO beginner: In the following implementation of a search results module I map the searchData JSON array to my view model using the mapping plugin. KO初学者:在搜索结果模块的以下实现中,我使用映射插件将searchData JSON数组映射到我的视图模型。 I also created two extra properties to display a manipulated version of some of my data: 我还创建了两个额外的属性来显示某些数据的受控版本:

define('searchresults', ['ko', 'lodash', 'datacontext', 'moment'], function (ko, _, datacontext, moment) {

    var get = function (term) {
        amplify.request("appsearch", { 'searchterm': term }, function (searchData) {

            var viewModel = {};

            var mapping = {
                'untilNow' : {
                    create: function (options) {
                        var innerModel = ko.mapping.fromJS(options.data);
                        innerModel.untilNow = moment(innerModel.lastSentDate()).fromNow();
                        return innerModel;
                    }
                },
                'iconClass': {
                    create: function (options) {
                        var innerModel = ko.mapping.fromJS(options.data);
                        innerModel.iconClass = "icon-" + innerModel.type();
                        return innerModel;
                    }
                }
            };

            viewModel.searchResults = ko.mapping.fromJS(searchData, mapping.untilNow);
            ko.applyBindings(viewModel);
        });
    };
    return {
        get: get
    };
});

This gets called in order to populate the following template: 调用此命令是为了填充以下模板:

<div id="contacts" class="view animated fadeInLeft">

    <h3>Search results for {{#routeData}}{{term}}{{/routeData}}</h3>
    <ul data-bind="template: { name: 'searchresults-template', foreach: searchResults }"></ul>

</div>

<script type="text/html" id="searchresults-template">

    <!--<li data-bind="attr: {'class': iconClass}">-->
    <li>
        <h4><span data-bind="text: type"></span></h4>
        <p><b>When created:</b> <span data-bind="text: untilNow"></span></p>
        <p><b>By:</b> <span data-bind="text: createdBy"></span></p>        
        <p><b>Company:</b> <span data-bind="text: company"></span></p>
        <hr/>
    </li>

</script>

<script>
    require(['searchresults'], function (searchresults) {
        var searchTerm = "{{#routeData}}{{term}}{{/routeData}}";
        searchresults.get(searchTerm);
    });
</script>

What I can't understand is: 我不明白的是:

  • Why in mapping.untilNow I can't just use mapping as apparently KO only expects an {}. 为什么在mapping.untilNow我不能只使用映射,因为KO显然只希望{}。 Which consequently I can't use the iconClass as it becomes undefined. 因此,由于iconClass变得未定义,因此我无法使用它。
  • What am I doing wrong that I have to repeat the innerModel implementation. 我在做错什么,我必须重复innerModel实现。 How can I abstract that out of the properties KO style! 我如何才能从KO样式属性中抽象出来!
  • Also I find it a bit dodgy that I have to assign the ko.mapping to the viewModel.searchResults and not just the viewModel but its the only way I could have something for mt template to iterate through. 另外,我还发现我不得不将ko.mapping分配给viewModel.searchResults,而不只是viewModel,这是我可以让mt模板进行迭代的唯一方式,这有点麻烦。

Am I not understanding some fundamental aspect of KO? 我不了解KO的一些基本方面吗? Is this the right way to get data and apply it to a template and add some methods to manipulate parts of it? 这是获取数据并将其应用于模板并添加一些方法来操纵其一部分的正确方法吗?

Thanks a bunch 谢谢一大堆

sample stream of of searchData: searchData的样本流:

[
    {
        "type": "Campaign",
        "lastSentDate": "/Date(634873003155215782)/",
        "createdBy": "Stephen Parker",
        "company": "Virgin Media"
    },
    {
        "type": "Person",
        "lastSentDate": "/Date(1198908717056-0700)/",
        "createdBy": "John Smith",
        "company": "Concep LTD"
    },
    {
        "type": "Campaign",
        "lastSentDate": "\/Date(1239018869048)\/",
        "createdBy": "Stephen Parker",
        "company": "Virgin Media"
    },
    {
        "type": "Company",
        "lastSentDate": "/Date(1198908717056-0700)/",
        "createdBy": "Stephen Parker",
        "company": "Virgin Media"
    }
]
define('searchresults', ['ko', 'lodash', 'datacontext', 'moment'], function (ko, _, datacontext, moment) {

    var get = function (term) {
        amplify.request("appsearch", { 'searchterm': term }, function (searchData) {

            var resultsItem = function (data) {
                var self = this;
                self.id = ko.observable(data.id);
                self.type = ko.observable(data.type);
                self.lastSentDate = ko.observable(data.lastSentDate);
                self.createdBy = ko.observable(data.createdBy);
                self.company = ko.observable(data.company);
                self.untilNow = ko.computed(function () {
                    return moment(self.lastSentDate()).fromNow();
                });
                self.iconClass = ko.computed(function() {
                    return "icon-" + self.type().toLowerCase();
                });
            };

            var dataMappingOptions = {
                key: function(data) {
                    return data.id;
                },
                create: function (options) {
                    return new resultsItem(options.data);
                }
            };

            var viewModel = {
                searchResults: ko.mapping.fromJS([])                
            };

            ko.mapping.fromJS(searchData, dataMappingOptions, viewModel.searchResults);
            ko.applyBindings(viewModel);
        });
    };
    return {
        get: get
    };
});

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

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