2012-09-24 35 views
0

我使用的Web应用程序淘汰赛与一个foreach (所有需要的库都包含)错误在淘汰赛映射JSON阅读

显示数据,我也得到了以下错误:

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 --> 

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

回答

3

这个问题很可能是您在使用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); 
+0

它的工作!我非常亲密,并且正在使用不相关的代码部分(例如data或data.d)。 非常感谢!我确信我应该使用fromJSON而不是fromJS。 因此,从JS阅读一个Javascript对象,而不是从一个JavaScript对象,因为KO需要JavaScript对象的工作。 – dtakis