剔除并非所有属性都被映射 [英] knockout not all properties are being mapped

查看:140
本文介绍了剔除并非所有属性都被映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对于淘汰赛专家来说,这对我来说是一个简单的问题,令人困惑.

I suppose that for the knockout experts this will be a simple question for me it's confusing.

我的JSON输入:

    {
      "odata.metadata":"http://localhost:49163/odata/$metadata#TwitterMessages","value":[
        {
          "message":{
            "text":"Pellentesque fermentum a diam a suscipit. Donec ultrices tempor sapien, vitae dictum erat pulvinar sagittis. Vivamus a fermentum lectus, et lacinia enim. Morbi pretium adipiscing risus, vitae euismod lacus fermentum eu.","company_fk":3,"image_url":"http://www.baconwrappedmedia.com/wp-content/uploads/2013/01/funny-kids-bacon-wrapped-media-21.jpg","active":true,"author_id":"3242342423","message_id":3,"author_picture":"http://3.bp.blogspot.com/_Gxh8CVH8l8Y/TJBDI4QVfnI/AAAAAAAAAWE/W1H4Z2MAub0/s1600/547206_1271901770610.27res_400_483.jpg","author_name":"Ligula","source":"TWITTER","createddatefriendly":"1 day ago","message":"Ut at erat at mauris euismod tincidunt non id justo?","pubdate":"2014-01-10T13:27:42.917"
          },"hashtag":{
            "hashtag_id":2,"hashtag":"#voetbal","company_fk":3
          },"twitter_id":"0","user_screenname":"Ligula","user_name":"Ligula","message_fk":3,"hashtag_fk":2
        },{
          "message":{
            "text":"Duis sed nunc semper, volutpat urna et, volutpat diam.","company_fk":3,"image_url":"http://www.allfunnystuff.com/wp-content/uploads/2013/04/funny-baby-angry-punch.jpg","active":true,"author_id":"75675675","message_id":4,"author_picture":"http://www.drodd.com/images8/funny-face15.jpeg","author_name":"Curabitur","source":"TWITTER","createddatefriendly":"3 day(s) ago","message":"Suspendisse a sapien id lorem tincidunt imperdiet eget id velit.","pubdate":"2014-01-10T13:30:26.657"
          },"hashtag":{
            "hashtag_id":2,"hashtag":"#voetbal","company_fk":3
          },"twitter_id":"0","user_screenname":"Curabitur","user_name":"Curabitur","message_fk":4,"hashtag_fk":2
        }
      ]
    }

我的javascript/淘汰代码:

My javascript/Knockout code:

    function CompanyViewModel() {
        var self = this;
        self.name = ko.observable();
    }

    function TweetViewModel(item) {
        var self = this;
        self.user_screenname = ko.observable("");
        self.user_name = ko.observable("");
        self.message = ko.observable("");
        self.hashtag = ko.observable("");
        ko.mapping.fromJS(item, {}, self);
    }

    function ViewModel() {
        var self = this;
        self.Company = ko.observable(new CompanyViewModel());
        self.Tweets = ko.observableArray([new TweetViewModel()]);

        $.getJSON('/Data/company.json', function (data) {
            ko.mapping.fromJS(data, {}, self.Company());
        });

        $.getJSON('/Data/tweets.json', function (allData) {
            var mappedTweets = $.map(allData.value, function (item) {
                return new TweetViewModel(item)
            });
            self.Tweets(mappedTweets);

            console.log(self.Tweets())
        });
    }

    var vm = new ViewModel();

    ko.applyBindings(vm);

我的HTML:

    <tbody data-bind="foreach: Tweets">
        <tr>
            <td data-bind="text: user_screenname"></td>
            <td data-bind="text: message().text"></td>
            <td data-bind="text: hashtag().hashtag"></td>
        </tr>
    </tbody>

一切正常.但这是我的问题:

Everything is working fine. But here is my question:

为什么我不需要声明" Message.Text是可观察的,以便将其用于数据绑定,但是我需要声明user_screenname是可观察的?

Why i DON'T need to "declare" the Message.Text observable to use it for databinding but i DO NEED to declare the user_screenname observable?

如果我要删除该行:

        self.user_screenname = ko.observable("");

我得到一个错误:未定义user_screenname.当我使用映射结构时,我希望user_screenname属性也能自动映射到viewmodel.还是我做错了什么?

I get an error: user_screenname is not defined. As i use the mapping construction i would expect that user_screenname property is also automatically mapped to the viewmodel. Or am i doing something wrong?

推荐答案

您需要明确声明user_screenname,因为返回数据中的message之一不包含user_screenname属性,因此映射不创建它,您的绑定将中断.

You need to declare explicitly your user_screenname because one of your message in the returned data does not contain the user_screenname property so the mapping does not create it and your binding will break.

您可以在属性的前面加上$data:

You can prefix your property with $data:

<td data-bind="text: $data.user_screenname"></td>` 

在这种情况下,如果您没有user_screenname属性,绑定不会中断,因此无需显式声明user_screenname即可使用.

in this case the binding won't break if you don't have an user_screenname property so it will work without explicitly declaring the user_screenname.

这篇关于剔除并非所有属性都被映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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