【问题标题】:Error in knockout mapping JSON read敲除映射 JSON 读取时出错
【发布时间】:2012-09-16 11:09:42
【问题描述】:

我在使用 foreach 显示数据的 Web 应用程序上使用敲除 (包括所有需要的库)

我收到以下错误:

Error: Unable to parse bindings. Message: ReferenceError: d is not defined; Bindings value: foreach: d
[Break On This Error]   

...+c+" } ";return new Function("sc",c)},kb:function(a,b){if(b.compareDocumentPosit...

我的 JSON:

{"d":[{"__type":"listingItem:#applicationModel","award_text":"","channel_id":5,"constructed_short_descriptionen":"","constructed_short_descriptiongr":"","constructed_titleen":"","constructed_titlegr":"","country_id"...

我的代码:

var Listing_ViewModel = {};

    var data = $.getJSON("listings.svc/Listing_Get_Items?",
                {
                    startdate: "2012-09-21 00:00:00",
                    stopdate: "2012-09-22 00:00:00",
                    channel_id: "5"
                }, function (data) {
                    // Now use this data to update your view models, 
                    // and Knockout will update your UI automatically 
                    //var parsed = JSON.parse(data);
                    alert(data.d[0].duration);
                    Listing_ViewModel = ko.mapping.fromJSON(data);
                    //alert(ko.mapping.toJS(Listing_ViewModel));
                    ko.applyBindings(Listing_ViewModel);
                    //        ko.applyBindings({
                    //          
                    //        });
                })

HTML 代码:

<!-- ko foreach: d -->
    <li class="item item-even" id="id_1" title="1">
    </li>
<!-- /ko -->

任何想法我做错了什么?对不起,我完全是绿色的!

【问题讨论】:

    标签: json knockout.js mapping


    【解决方案1】:

    问题很可能是您使用的是fromJSON,而您的真正意思是fromJS$.getJSON 已经为您创建了一个对象,而ko.mapping.fromJSON 读取了一个 JSON 字符串。 ko.mapping.fromJS 读入一个 Javascript 对象。

    查看此 JSFiddle 了解更多信息:http://jsfiddle.net/hB3Rm/

    基本上,改变这一行:

    Listing_ViewModel = ko.mapping.fromJSON(data);
    

    到这一行:

    Listing_ViewModel = ko.mapping.fromJS(data);
    

    【讨论】:

    • 成功了!我是如此接近并且正在玩弄不相关的代码部分(例如数据或data.d)。非常感谢!我确信我应该使用 fromJSON 而不是 fromJS。所以 fromJS 读取的是 Javascript 对象而不是 Javascript 对象,因为 KO 需要 javascript 对象才能工作。
    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 2013-07-17
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多